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];
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.
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
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
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With