Had a bit of problem this week where I needed to override the background image in a UINavigationController (and support iOS 4.3 and iOS 5.1).

Turns out the solution is create a new UINavigationBar subclass and then use it in the UINavigation controller.

Only problem is, the navigationBar property is readonly on UINavigationController. You can’t set it.

Then this handy post showed my how you could set the readonly value using KVC.

[self.navigationController setValue:[[[CustomNavBar alloc]init] autorelease] forKeyPath:@"navigationBar"];

Very handy! Now I can create subclass:

#import "CustomNavigationBar.h"

static UIImage *portraitNavBar = nil;

@implementation CustomNavigationBar

- (id)initWithFrame:(CGRect)frame
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    return self;

- (void)drawRect:(CGRect)rect
    if (!portraitNavBar)
        portraitNavBar = [UIImage imageNamed:@"HeaderBg_320x44.png"];
    [portraitNavBar drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];


And set it as shown above. Nice…