Rogare
Rogare

Reputation: 3274

What to subclass (if anything) to customization UINavigationBar's appearance?

I'd like to customize my UINavigationBar's view by adding a custom background colour, back icon, and button on the right side. I've seen various solutions for this, and am looking to support iOS7 and iOS8+. Where's the standard place to do this:

Thanks for reading!

Upvotes: 0

Views: 65

Answers (1)

user2535467
user2535467

Reputation:

There are a few ways that you could do this, although the only reference I could provide for choosing one over the other is the example that Apple gives.

The Navigation Controller and it's UI Bar are very customizable, there is almost nothing that can't be changed (from a UI perspective).

There are several examples which just say dump the code in the AppDelegate. I have done so myself. While I can not say I have had problems with it, it did seem a bit odd/cluttered.

So in this case, I would do it the way Apple shows us all how to do it and add the customizations to the .m file of the VC that you want to see the customizations on (you should download the sample project and play with it).

Doing it this way seems to be a bit cleaner and you can specify which VC gets which look (instead of making all VC's have the same nav bar).

To supply some code showing how to do your requested customizations:

Custom background color:

[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];

Custom back icon:

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"some_btn_image.png"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"some_btn_image.png"]];

Button on the right side:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Email"]
                                                                  style:UIBarButtonItemStyleBordered target:self action:@selector(action:)];

self.navigationItem.rightBarButtonItem = addButton;

I answered this to the best of my abilities with the data that I have. I figured a generally decent answer is better than no answer (I saw you were getting a bit impatient.)

Upvotes: 1

Related Questions