2

The appearance proxy is useful as long as you don't use any UI elements provided by the OS, such as the MFMailComposeViewController class. For this reason, I need to customize the bar button items in the navigation bar, the back button in particular.

What is the problem? This seems problematic when it comes to modifying the title attributes of the back button. Quite a bit has been written about customizing the back button, but I can't seem to find information about modifying the title attributes (text color, shadow color, etc.) of the back button (without using the appearance proxy).

What have I tried? Setting the title attributes of the backBarButtonItem property as shown below. It doesn't matter if I do this in the child or parent view controller. This works fine for the leftBarButtonItem and `rightBarButtonItem' items, which is what confuses me.

[self.navigationItem.backBarButtonItem setTitleTextAttributes:titleAttributes forState:UIControlStateNormal];

Of course, using the appearance proxy works fine as well (see below), but this messes up navigation bars provided by the OS as mentioned above.

[[UIBarButtonItem appearance] setTitleTextAttributes:titleAttributes forState:UIControlStateNormal];
Community
  • 1
  • 1
Bart Jacobs
  • 9,022
  • 7
  • 47
  • 88
  • The problem is, the `backBarButtonItem` property itself is `nil`! So, passing any message to it will have no effect. As @βhargavḯ points out, this is in coherence with the documentation. So, without using the `appearance` API, you could create a `UIBarButtonItem` instance with the desired customizations in code and set it as the back button instead. – trss May 07 '14 at 17:25

2 Answers2

2

You can target the appearance of UIBarButtonItems only when they exists within a certain view hierarchy. So you could create a subclass of UINavigationController, say MyNavigationController and do:

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

Mike Pollard
  • 10,195
  • 2
  • 37
  • 46
1

I can't seem to find information about modifying the title attributes (text color, shadow color, etc.) of the back button - of course you will not find.

As per UINavigationItem Class Reference

If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.

βhargavḯ
  • 9,786
  • 1
  • 37
  • 59