UINavigationBar title overlaps momentarily on VC load with Custom NavBar Height - ios

I have a custom method in my AppDelegate:
-(void)setupNavBar{
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
[nav.navigationBar setBounds:CGRectMake(0,0,320,70)];
nav.navigationBar.translucent = YES;
[nav.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
nav.navigationBar.shadowImage = [UIImage new];
nav.navigationBar.translucent = YES;
nav.view.backgroundColor = [UIColor clearColor];
UIView *navBorder = [[UIView alloc] initWithFrame:CGRectMake(0, nav.navigationBar.frame.size.height-1, nav.navigationBar.frame.size.width, 1)];
// Change the frame size to suit yours //
[navBorder setBackgroundColor:[UIColor colorWithWhite:200.0f/255.f alpha:0.8f]];
[navBorder setOpaque:YES];
[nav.navigationBar addSubview:navBorder];
CGFloat verticalOffset = 25;
[nav.navigationBar setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTintColor:[UIColor clearColor]];
[[UINavigationBar appearance] setTitleTextAttributes:#{
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeFont: [UIFont fontWithName:#"Dosis-ExtraBold" size:33.0],
}];
}
I am presenting using a Nav Controller on storyboard and on the initial view controller everything looks great. However, when I push another VC (via segue) the title only half shows before the Navigationbar area seems to resize and redraw. Is there anyway to force a redraw in the ViewWillAppear so that the title text shows up whole straight away?
Have attached some images of the transition.
Thanks.
http://puu.sh/7AkJL

Try to use this:
- (void)viewDidLoad
{
//your code
self.navigationItem.titleView = [self initTitle: #"IN HAND"];
}
- (UIView *)initTitle:(NSString *) titleString
{
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 44)]; //your size!
UILabel *navBarLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 44)];
navBarLabel.text = titleString;
[navBarLabel setTextAlignment:NSTextAlignmentCenter];
navBarLabel.font = [UIFont fontWithName:#"Menlo-Regular" size:16.0f];
navBarLabel.textColor = [UIColor whiteColor];
[titleView addSubview:navBarLabel];
return titleView;
}

Related

Get the real color of the navigation bar

For my app on iPhone X I need to create a footer. No problem here.
BUT I need to put the same color as the navigation bar.
The color of the navigationBar is set on barTintColor and cannot be changed.
The app builded, a UIVisualEffect is set automatically on the navigationBar and the color change : it's lighter.
How can I get the color shown of the navigation bar?
What I tried but didn't work :
footer.backgroundColor = [UIColor myColor]
footer.tintColor = [UIColor myColor]
footer.tintColor = navigationBar.barTintColor
footer.backgroundColor = navigationBar.barTintColor
imageView.image = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault]; [footer addSubview: imageView];
UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView* visualView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
[self.footer addSubview:visualView];
**The code for the navigation bar : **
self.navigationController.navigationBar.barTintColor = tintColor;
[self.navigationController.navigationBar setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont dpBoldFontWithSize:16.8],NSFontAttributeName,
fontColor,NSForegroundColorAttributeName,
nil]];
[self.navigationItem setTitle:title];
**The code for the footer **
self.footerView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 35, self.view.width, 35)];
[self.footerView setBackgroundColor:color];
[self.view addSubview:self.footerView];
Screenshot of the problem :
Screenshot
Or what about autoresizing mask ?
UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 35, self.view.bounds.size.width, 35)];
footerView.backgroundColor = UIColor.redColor;
footerView.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth);
[self.view addSubview:footerView];

CNContactPickerViewcontroller navigationBar color

how do i change the black bar to another color?
N.B this is a CNContactPickerViewcontroller...the first screen(list of contacts) looks fine but when i click on a contact to choose a specific contact property then the navigation bar turns black.
Thanks
One way is to set the Appearance of UINavigationBar to the color you want:
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
And once you return to the previous ViewController (Maybe with -(void)viewWillAppear:(BOOL)animated) you set it again to the previous color you were using.
I presented the Contact this way:
CNContactStore *store = [[CNContactStore alloc] init];
// Create contact
CNMutableContact *contact = [[CNMutableContact alloc] init];
contact.givenName = #"Someone Name";
CNLabeledValue *contactPhoneNumber = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:#"Some number"]];
contact.phoneNumbers = #[contactPhoneNumber];
CNContactViewController *contactController = [CNContactViewController viewControllerForUnknownContact:contact];
contactController.navigationItem.title = #"Add to contacts";
contactController.contactStore = store;
contactController.delegate = self;
[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor blackColor]};
[self.navigationController pushViewController:contactController animated:YES];
Once the contactController is Dismissed the viewWillAppear is called and you can add there the color restore depending on your needs:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor whiteColor]};
}
I've solved it by changing the backgroundColor of the navbar subviews after a delay, not pretty, but it's good enough for me:
CNContactViewController *vc = [CNContactViewController viewControllerForContact:contact];
vc.delegate = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
for (UIView *view in [vc.navigationController.navigationBar subviews]) {
view.tintColor = [UIColor darkTextColor];
view.backgroundColor = [UIColor redColor];
}
});
[self.navigationController pushViewController:vc animated:YES];

How to set image title for UIView for CAPSPageMenu in objective c

I am new in IOS and creating an app.
I used CAPSPageMenu for page menu.Menu is working fine but i want to change
title as a image. I searched a lot,but all are negative.
PlusTypeViewController *controller1 = [[PlusTypeViewController alloc]initWithNibName:#"PlusTableView" bundle:nil];
controller1.title = #"PLUSr";
CapsulesViewController *controller2 = [[CapsulesViewController alloc]initWithNibName:#"CapsulesView" bundle:nil];
controller2.title = #"SUBSTITUTEr";
TabletsViewController *controller3 = [[TabletsViewController alloc] initWithNibName:#"Tablets" bundle:nil];
controller3.title = #"SUPPLIERS";
InjectionViewController *controller4 = [[InjectionViewController alloc] initWithNibName:#"Injection" bundle:nil];
controller4.title = #"INJECTION";
ShasheViewController *controller5 = [[ShasheViewController alloc] initWithNibName:#"Shesha" bundle:nil];
controller5.title = #"Shashe";
NSArray *controllerArray2 = #[controller1, controller2,controller3,controller4,controller5];
NSDictionary *parameterss = #{
CAPSPageMenuOptionScrollMenuBackgroundColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
//CAPSPageMenuOptionViewBackgroundColor: [UIColor colorWithRed:19.0/255.0 green:112.0/255.0 blue:103.0/255.0 alpha:1.0],
//CAPSPageMenuOptionSelectionIndicatorColor: [UIColor greenColor],
CAPSPageMenuOptionSelectionIndicatorColor:[UIColor colorWithRed:19.0/255.0 green:112.0/255.0 blue:103.0/255.0 alpha:1.0],
CAPSPageMenuOptionUnselectedMenuItemLabelColor:[UIColor colorWithRed:19.0/255.0 green:112.0/255.0 blue:103.0/255.0 alpha:1.0],
//CAPSPageMenuOptionSelectionIndicatorColor:[UIColor greenColor],
CAPSPageMenuOptionSelectedMenuItemLabelColor :[UIColor blackColor],
//CAPSPageMenuOptionBottomMenuHairlineColor: [UIColor colorWithRed:19.0/255.0 green:112.0/255.0 blue:103.0/255.0 alpha:1.0],
//CAPSPageMenuOptionBottomMenuHairlineColor: [UIColor blackColor],
CAPSPageMenuOptionMenuItemFont: [UIFont fontWithName:#"HelveticaNeue" size:13.0],
CAPSPageMenuOptionMenuHeight: #(30.0),
CAPSPageMenuOptionMenuItemWidth: #(90.0),
//CAPSPageMenuOptionCenterMenuItems: #(YES)
};
_secondPageMenu = [[CAPSPageMenu alloc] initWithViewControllers:controllerArray2 frame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height) options:parameterss];
[self.view addSubview:_secondPageMenu.view];
PLUSr is a demo title, i want to change it in image. Thanks in advance.
#pragma mark:- Navigation Bar
-(void)createCustomNavBar
{
UIView *topBarView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
topBarView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
[self.view addSubview:topBarView];
UIButton *btnBack = [[UIButton alloc]initWithFrame:CGRectMake(2.5, 0, 44, 44)];
[btnBack setImage:[UIImage imageNamed:#"backs_icon_iPhone.png"] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
[topBarView addSubview:btnBack];
UIImageView *titleImageView=[[UIImageView alloc]init];
titleImageView.image= [UIImage imageNamed:#"sample.png"];
titleImageView.frame=CGRectMake(80, 0, SCREEN_WIDTH-160, 44);
[topBarView addSubview:titleImageView];
}
this is one of the solution. call this createCustomNavBar mathod. thanks
You can set image as a title of controller like below code,
First you need to embed Navigation Controller for navigate on controller and then write below code in viewDidLoad method of that controller
UIImage *image = [[UIImage alloc]initWithName:#"YOUR_IMAGE"];
UIImageView *imageView = [[UIImageView alloc]init];
imageView.image = image;
self.navigationItem.titleView = imageView;
Hope this will help you :)
NOTE : I am doing like this in Swift but if any correction for obj-c then pls change it.
Navigationitem has titleView property which can be used to set up custom view in our navigation view.
You can make a method like this and call in you viewDidLoad.
-(void)setTitleViewForNavigation {
self.navigationController.navigationBar.backgroundColor =[UIColor blueColor];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 44)];
[imageView setImage:[UIImage imageNamed:#"placeholder"]];
imageView.backgroundColor = [UIColor redColor];
self.navigationItem.titleView = imageView;
[self.navigationItem setTitleView:imageView];
}
By using:
CAPSPageMenuOptionScrollMenuBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImage"]

Remove between UITableView and UINavigationBar

I have an UITableView which I am trying to make the background color of the first cell match the color of the UINavigationBar, as seen in the screenshot below.
However, as you can see, there is a dark border between both objects which I don't know where is coming from.
Moreover, it is evident that the tone of the color used is not the same in both cases, despite using the same [UIColor colorWithRed:] code. This is not the main problem, but I wanted to mention this too.
Any thoughts?
EDIT
viewDidLoad:
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.0];
self.tableView.scrollEnabled = NO;
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.clipsToBounds = YES;
self.navigationController.navigationBar.translucent = NO;
cellForRowAtIndexPath:
cell.backgroundColor = myColor;
Try this:
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
But if you want to remove the hairline alone and keeping the blur effect, try:
self.navigationController.navigationBar.clipsToBounds = YES;
and by the way, this is my actual code of testing:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *red = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 44, 44)];
red.backgroundColor = [UIColor redColor];
[self.view addSubview:red];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.clipsToBounds = YES;
}
Sample output be:
Update:
Looks like -clipToBounds do not suits your implementation.
Here what i think suits yours:
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 green:0.5 blue:0.97 alpha:1.0];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.translucent = NO;
Before: After:
Note: The [UIColor colorWithRed:0 green:0.5 blue:0.97 alpha:1.0] is just for example, assign your desired color to .barTintColor.
Perhaps
//0084D3
UIColor * color = [UIColor colorWithRed:0/255.0f green:132/255.0f blue:211/255.0f alpha:1.0f];
//006FCC
UIColor * color = [UIColor colorWithRed:0/255.0f green:111/255.0f blue:204/255.0f alpha:1.0f];
is what you need based from UIColor Code Generator :)
Hope this is helpful.. Cheers.. :)
The line you're seeing is the navigation bar's shadowImage. You can remove it by adding
self.navigationController.navigationBar.shadowImage = [UIImage new];
EDIT
Not sure if you've solved this already, but I set up a brand new project with a UITableViewController embedded in a UINavigationController and there's no line under the nav bar. Here's the configuration I did:
In viewDidLoad:
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setBarTintColor:myColor];
self.tableView.backgroundColor = myColor;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
In tableView:cellForRowAtIndexPath:
cell.contentView.backgroundColor = myColor;
cell.textLabel.backgroundColor = myColor;
cell.textLabel.text = #"this";
Nothing special in Storyboard.

removing uinavigationbar title margins

I modified my navigation bar as follows in app delegate:
NSDictionary *settings = #{
UITextAttributeFont : [UIFont fontWithName:#"impact" size:36.0],
UITextAttributeTextColor : [UIColor whiteColor],
UITextAttributeTextShadowColor : [UIColor clearColor],
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetZero]};
[[UINavigationBar appearance] setTitleTextAttributes:settings];
But the font cuts down like this for the larger font. :
I tried doing this in viewWillAppear of my VC, :
UIView *newTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[newTitleView setBackgroundColor:[UIColor blackColor]];
[self.navigationController.navigationBar addSubview:newTitleView];
Then I was going to align title to center. But that just doesn't seems right. Only thing I actually need is to remove margins at top and bottom of the title Label. How do I go about it.
Try this code and don't set setTitleTextAttributes
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectZero];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.textColor = [UIColor yellowColor]; // Change to desired color
self.navigationController.navigationItem.titleView = titleView;
[titleView release];
}
titleView.text = title;
[titleView sizeToFit];
Well the easiest way i would suggest is to use a UIView with label and button customised for the requirement and use it,not on navigation bar.
mnavigation bar height customization is the trouble that the height is fixed and the subview also it may affect .So I suggest to go with a custom header view subclassed with UIView
You can subclass to the UILabel and override drawTextInRect: like this:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
For more useful link you can find
here

Resources