gbesler gbesler - 8 days ago 11
iOS Question

Navigation bar back button text is not changing

I am trying to change the text of the navigation bar back button text. I have tried the solutions offered here but I cant get the text I want on back button. In appdelegate I set these features:

[[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:@"e74c3c"]];
[[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:@"e74c3c"]];

[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont fontWithName:@"Futura-CondensedMedium" size:19.0], UITextAttributeFont,nil]];

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor whiteColor],
NSShadowAttributeName:shadow,
NSFontAttributeName:[UIFont fontWithName:@"Futura-CondensedMedium" size:19.0]
}
forState:UIControlStateNormal];

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];


And in every viewcontroller's
viewdidload
I set:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"SomeText" style:UIBarButtonItemStylePlain target:nil action:nil];


or

UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:@"NewTitle"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];


Any suggestions?

Answer

If I haven't misunderstood you you can do this to set the title of the back button. Suppose there are three view controllers: A, B and C. Now you want the title of the back button to be "MyViewA" when you navigate from A to B then you need to put this code in "viewWillAppear:" of your ViewController A (this will show the title of the back button as "MyViewA" when you are on view controller B).

 //viewWillAppear of ViewController A
 -(void)viewWillAppear:(BOOL)animated{

       //now set title of back button
       self.navigationItem.backBarButtonItem =
       [[UIBarButtonItem alloc] initWithTitle:@"YourTitle"
                                  style:UIBarButtonItemStyleBordered
                                 target:nil
                                 action:nil];
 }

Likewise if you are navigating from B to C and want to set the back button title for ViewController C, you need to put the above code in "viewWillAppear:" delegate of ViewController B. In short you need to set the title of the next view controller on your current view controller.

Comments