Title of UINavigationBar becomes blank when segueing back to root VC - ios

I've got a UINavigationController that segues to 2 other VC's. Whenever it reaches a certain VC with the current setup :
// set text
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
self.title = #"Title";
// make the nav bar transaparent
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
[self.navigationController.navigationBar setOpaque:NO];
// set white status bar
self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
[self setNeedsStatusBarAppearanceUpdate];
//self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(16, 31, 22, 20)];
UIImage *backImage = [UIImage imageNamed:#"backButtonDetail"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
And I pop the VC, the title of the root vc becomes empty. However, whenever I'm segueing to a VC without this setup, but a more 'basic' setup, the title stays where it should.

Easiest thing I can recommend: use [UINavigationBar appearance] to set many of these customizations from the AppDelegate
Checkout out AppCoda's post on the subject. I've just run through it myself and it's pretty great

Related

iOS - Change only backBarButtonItem text color on a navigation bar

I'm trying to change the color of the backBarButtonItem on a navigation bar in my app (iOS 9+).
I can do this with :
[[UIBarButtonItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
But this will change the color for all UIBarButtonItems on the navigation bar and I only want to change the back button.
I have tried :
[self.navigationItem.leftBarButtonItem setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
and
[self.navigationItem.leftBarButtonItem setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
But it doesn't work
NOTE: I want to keep the < arrow of the system back button so using a custom view is not an option unless I use a custom image
In the end I used the following solution:
1. Set the general appearance of UIBarbuttonitem BEFORE adding the right bar button item
[[UIBarButtonItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor blueColor]} forState:UIControlStateNormal];
2. Then set the right bar button item and its specific appearance
UIBarButtonItem *rigthBtn = [[UIBarButtonItem alloc] initWithTitle:#"title" style:UIBarButtonItemStylePlain target:self action:#selector(rightBtnTapped:)];
[rigthBtn setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
[self.navigationItem setRightBarButtonItem:rigthBtn];
As per my comment posting sample code
UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom];
useButton.frame = CGRectMake(0, 0, 20,17);
useButton.layer.masksToBounds = NO;
useButton.layer.shadowOffset = CGSizeMake(3, 3);
useButton.layer.shadowRadius = 3;
useButton.layer.shadowOpacity = 0.1;
useButton.layer.shadowColor = [UIColor blackColor].CGColor;
useButton.backgroundColor = [UIColor clearColor];
useButton.tintColor = [UIColor whiteColor];
[useButton setImage:[UIImage imageNamed:#"image if any"] forState:0];
[useButton addTarget:self action:#selector(btnBackTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:useButton]];
`
One year too late but did you tried to set the tintColor? I dont know your syntax but in swift it would be:
navigationController.navigationBar.tintColor = UIColor.COLOR

reusable UIBarButtonItem in all View Controllers in Objective C

I making custom navigationBar with background color and font size. In addition i have add menu button on right. For this I had made the category named
UINavigationController+Transparent.h
#interface UINavigationController (Transparent)
- (void)presentTransparentNavigationBar;
- (void)hideTransparentNavigationBar;
#end
UINavigationController+Transparent.m
#import "UINavigationController+Transparent.h"
#implementation UINavigationController (Transparent)
UIBarButtonItem *menuButton;
- (void)presentTransparentNavigationBar;
{
menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"menu_icon"] style:UIBarButtonItemStylePlain target:self action:#selector(showMenu:)];
[menuButton setTintColor:[UIColor whiteColor]];
self.navigationItem.rightBarButtonItem = menuButton;
UIImage *backButtonImage = [UIImage imageNamed:#"back"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage
forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self
action:#selector(popViewController)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
[self.navigationBar setTranslucent:NO];
[self.navigationBar setShadowImage:[UIImage new]];
[self.navigationBar setBarTintColor:[UIColor wolfRed]];
[self.navigationBar setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:#"Helvetica" size:19.0] }];
[self setNavigationBarHidden:NO animated:NO];
}
- (void)hideTransparentNavigationBar
{
[self setNavigationBarHidden:YES animated:NO];
[self.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]];
[self.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]];
}
#end
inside my ViewControllers i call
[self.navigationController presentTransparentNavigationBar];
or
[self.navigationController hideTransparentNavigationBar];
The problem is that menu button is not visible and back button is seems like standard iOS blue back button.
Thanks for help!
See I used to customize my navigation bar in following way with
objective c
Step.1 Make sure your View Controller is embed in Navigation Controller
Step.2 Add required icons in your assets eg. "menu_icon" and "back"
Step.3 with the objective-C you can customize navigation bar as like you want.
I always do this way,
#import "ViewController.h"
#interface ViewController (){UIBarButtonItem *menuButton;}
#end
#implementation ViewController
#pragma mark - life cycle methods
- (void)viewDidLoad {[super viewDidLoad];}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}
#pragma mark - IBAction methods
- (IBAction)showClicked:(id)sender {[self presentTransparentNavigationBar];}
- (IBAction)hideClicked:(id)sender {[self hideTransparentNavigationBar];}
#pragma mark - Navigation Bar methods
//Customize Naviagtion Bar
- (void)presentTransparentNavigationBar;
{
//Add Menu Button to Navigaiton Bar
menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"menu_icon"] style:UIBarButtonItemStylePlain target:self action:#selector(showMenu:)];
[menuButton setTintColor:[UIColor whiteColor]];
self.navigationItem.rightBarButtonItem = menuButton;
//Add Back button to Navigation Bar
UIImage *backButtonImage = [UIImage imageNamed:#"back"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage
forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self
action:#selector(popViewController)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
//Config Navigaton bar settings
[self.navigationController.navigationBar setTranslucent:NO];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
[self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];//I used light gray color
[self.navigationController.navigationBar setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:#"Helvetica" size:19.0] }];
[self.navigationController.navigationBar setHidden:NO];
}
//Hide Navigation Bar
- (void)hideTransparentNavigationBar
{
[self.navigationController.navigationBar setHidden:YES];
[self.navigationController.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]];
[self.navigationController.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]];
}
- (void) showMenu:(id) sender{ /*TODO when clicks on Menu button */ }
- (void) popViewController{ /*TODO when clicks on Back button */ }
#end
and if managed everything proper you should be able to get Menu and Back button to navigation bar when you click show button on the screen.
Note:
you can do this in your AppDelegate file so it will available in entire application

Why does my custom Navigation bar back button overlap/duplicated (Xcode 7.0)?

I added the following to customize my back button in my navigation bar but the image just overlaps/duplicated:
http://i.stack.imgur.com/eyzJC.png
- (void)viewWillAppear:(BOOL)animated
{
//take out the label following the button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
//set image for back button
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"backButtonImage.png"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"backButtonImage.png"]];
self.navigationItem.backBarButtonItem = backButton;
}
It's supposed to be an image of one black arrow facing to the left.
Before it worked fine but now it does this when I simulate it. Any insight and help would be appreciated.
Thank you
This should work for you.
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(0, 0, 50, 44);
back.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[back addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
[back setImage:[UIImage imageNamed:#"backButtonImage.png"] forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: back];
add a function:
-(void) back
{
[self.navigationController popViewControllerAnimated:YES];
}
Add this code
[UINavigationBar appearance].barTintColor = [UIColor colorWithWhite:1.00 alpha:1];
It should solve your problem.

Transition to UINavigationBar with transparent background image results in black flickering

I want to navigate from a white UINavigatinonBarto a transparent UINavigationBar.
In my root view controller, this is the setup:
self.navigationController.navigationBar.hidden = NO;
[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setTranslucent:NO];
It's important that it stays non translucent.
This is the setup in the second view controller:
// set title of navbar
self.title = [self.data objectForKey:#"title"];
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
//
// IMPORTANT PART: make the nav bar transaparent - no prerequisites.
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
// set custom back button
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(16, 31, 22, 20)];
UIImage *backImage = [UIImage imageNamed:#"backButtonDetail"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
This goes almost well, except for the animation:
As you can see, the background becomes black when I animate it. I want to fade the white into the destination color. It now animates from the black background.
I tried settings a custom UIView underneath the nav bar, but that didn't work.
I'm quite lost about how to about this now and any help would be highly appreciated.
I recently ran into this same problem and was able to get around it by just setting the window's background color in the appDelegate's didFinishLaunchingWithOptions
window.backgroundColor = UIColor.whiteColor()
and if you don't already have a reference to window then
UIApplication.sharedApplication().delegate?.window.backgroundColor = ...
I found this to be the cleanest solution. Hope it helps, albeit a bit late.

Setting image for UINavigationitem (Leftbarbutton item) in ios

I m trying to setting image for UIBarButtonItem using the below code. But it appears as a blank image in the navigation bar. please help me to resolve the problem. i m entirely new to ios.
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:38.0/255.0 green:126.0/255.0 blue:202.0/255.0 alpha:0.5]];
UIBarButtonItem *lftbarbtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"Cross_Icon.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(additem)];
[self.navigationItem setLeftBarButtonItem:lftbarbtn];
thank u in advance.......
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(popToBack)]; //create the left bar button
self.navigationItem.leftBarButtonItem = leftBarButton; //assign into navigation item
self.navigationItem.leftBarButtonItem.tintColor=[UIColor colorWithRed:125.0/255.0 green:90.0/255.0 blue:146.0/255.0 alpha:1]; //set the tint color
[leftBarButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor purpleColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; //text color
-(void)popToBack
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
in another choice
UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftbutton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[leftbutton addTarget:target action:#selector(buttonAction)forControlEvents:UIControlEventTouchUpInside];
[leftbutton setFrame:CGRectMake(0, 0, 53, 31)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(3, 5, 50, 20)];
[label setFont:[UIFont fontWithName:#"Arial-BoldMT" size:13]];
[label setText:title];
label.textAlignment = UITextAlignmentCenter;
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[leftbutton addSubview:label];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView: leftbutton];
self.navigationItem.leftBarButtonItem = barButton;
-(void) buttonAction
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
You do not need to use the navigation bar directly. Use the current view controller's navigation item property to set the UIBarButtomItems. Make sure you are actually using a navigationController to set these properties.
UIButton *ReqMenuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
ReqMenuBtn.frame = CGRectMake(0,0,32,32);
[ReqMenuBtn setBackgroundImage:[UIImage imageNamed:#"reselect.png"] forState:UIControlStateNormal];
[ReqMenuBtn addTarget:self action:#selector(somemethodName:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *MenuButton = [[UIBarButtonItem alloc]initWithCustomView:ReqMenuBtn];
[self.navigationItem setLeftBarButtonItem:MenuButton];
MenuButton = nil;
ReqMenuBtn = nil;

Resources