5

I'd like to change the background of my UINavigationBar to a [UIColor colorWithImage:], but it isn't working. What am I missing?

EDIT:

Once I've created my subclass, where do I set the UINavigationController to use it?

Moshe
  • 57,511
  • 78
  • 272
  • 425

2 Answers2

8

You can use the tintColor property to change the colour of a UINavigationBar, but to set an image as the background you'll have to provide your own UINavigationBar subclass and override the drawRect: method, for example:

- (void)drawRect:(CGRect)rect {
    // Drawing code 
    UIImage *img = [UIImage imageNamed: @"background-image.png"];
    [img drawInRect:CGRectMake(0, 
                               0, 
                               self.frame.size.width, 
                               self.frame.size.height)];
}

If you use Interface Builder to build your UI then to use the custom navigation bar, just select the UINavigationBar element in Interface Builder, open the Inspector and in the Identity tab specify your UINavigationBar subclass in the class field, like so:

Example screenshot showing custom UINavigationBar subclass

Simon Whitaker
  • 20,506
  • 4
  • 62
  • 79
  • Now how do I set the UINavigationBar to show inside of the UINavigationController? – Moshe Dec 02 '10 at 00:03
  • Good question - answer amended accordingly. – Simon Whitaker Dec 02 '10 at 09:04
  • Is this possible purely in code? Also, does this break HIG at all? – Moshe Dec 02 '10 at 20:21
  • I don't believe it breaks HIG. I have apps on the App Store that use this. – Simon Whitaker Dec 02 '10 at 20:45
  • It looks as though setting it in code isn't possible, unless you use UINavigationController's undocumented `setNavigationBar:` method, which works but is likely to get your app rejected from the App Store. You can use Denis Hennessy's category solution (one of the other answers here) to achieve this purely in code. [This SO question](http://stackoverflow.com/questions/1869331/set-programmatically-a-custom-subclass-of-uinavigationbar-in-uinavigationcontroll) has some other suggestions. – Simon Whitaker Dec 02 '10 at 21:01
7

To have an image in the navigation bar, you have to draw it yourself, which actually isn't that hard. Save this as UINavigationBar+CustomBackground.m (it adds a custom category to UINavigationBar):

@implementation UINavigationBar (CustomBackground)

- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"NavMain.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end
Denis Hennessy
  • 7,243
  • 4
  • 25
  • 31
  • 1
    Using a category would irrevocably change the background of *all* navigation bars. You're better off using a subclass. – Adam Milligan Nov 30 '10 at 22:55
  • we used this method until we wanted different navigation bars on different view controllers. Then this method unfortunately fails – Kostiantyn Sokolinskyi Jun 06 '11 at 15:02
  • Actually folks, you may be able to do some class detection and behave differently based on the class. You still have to reimplement the default behaviors though. – Moshe Aug 10 '11 at 13:23