Bart Jacobs
Bart Jacobs

Reputation: 9082

Customize title attributes of specific back button

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];

Upvotes: 2

Views: 1000

Answers (2)

Mike Pollard
Mike Pollard

Reputation: 10195

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];

Upvotes: 2

βhargavḯ
βhargavḯ

Reputation: 9836

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.

Upvotes: 1

Related Questions