Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use UIAppearance to set text color of back button for UIBarButtonItem?

Tags:

ios

iphone

ipad

I have been customizing my UIBarButtonItems using UIAppearance, but I don't know how to change the style of the text for the back button. How is this done?

I know how to set the background image:

[[UIBarButtonItem appearance]
   setBackButtonBackgroundImage:[UIImage imageNamed:@"arrow-button-static.png"]
   forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

  [[UIBarButtonItem appearance]
   setBackButtonBackgroundImage:[UIImage imageNamed:@"arrow-button-pressed.png"]
   forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
like image 583
Andrew Johnson Avatar asked Feb 04 '13 18:02

Andrew Johnson


4 Answers

You can't change the text attributes of only the Back button. (Or at least, you can't do so globally using the appearance proxy.) Apple apparently intends that its text match that of other buttons in the navigation bar, with only its shape (background image) differing.

As @DhruvGoel notes, you can set text attributes thusly:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                       [UIColor redColor],UITextAttributeTextColor,
                                       nil];

[[UIBarButtonItem appearance] setTitleTextAttributes:attributes                   
                          forState:UIControlStateNormal];

Or, for only bar buttons in the navigation bar:

    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
            setTitleTextAttributes:attributes                   
                          forState:UIControlStateNormal];

See the UIAppearance protocol docs for more details about how to customize appearance in specific parts of your view / view controller hierarchy. Though as I've said, this won't let you globally make Back button text different from that of other buttons in the same navigation bar. (You could go nuts directly changing each one as your view controllers appear, but that's probably not a great idea.)

Oh, and sorry about that impeachment thing.

like image 66
rickster Avatar answered Nov 06 '22 10:11

rickster


To change the back button item text color, you can use this:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                           [UIColor redColor],UITextAttributeTextColor,
                                           nil];

[[UIBarButtonItem appearance] setTitleTextAttributes:attributes                   
                              forState:UIControlStateNormal];

You may specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary, using the keys found in UIStringDrawing.h i.e. UITextAttributeFont, UITextAttributeTextColor, UITextAttributeTextShadowColor and UITextAttributeTextShadowOffset

Note that this is only iOS 5.0 onwards

like image 26
Dhruv Goel Avatar answered Nov 06 '22 09:11

Dhruv Goel


If you want to change the global color of both the text and the back button, here is a simple solution (in Swift 3):

UINavigationBar.appearance().tintColor = UIColor.blue
like image 31
dustinrwh Avatar answered Nov 06 '22 10:11

dustinrwh


You can customize your back bar button item using UIButton.

UIButton *backButton = [[UIButton alloc] initWithFrame:frame];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
backButton.titleLabel.shadowColor = [UIColor blackColor];
backButton.titleLabel.shadowOffset = CGSizeMake(0, 1);
backButton.titleLabel.textColor = [UIColor whiteColor];

[self.navigationItem setLeftBarButtonItem:backBarButton];
like image 1
Nikolay Tabunchenko Avatar answered Nov 06 '22 09:11

Nikolay Tabunchenko