Just as an additional thought, I managed to achieve a completely customized navigation bar by overriding drawRect:
to have it display a background image. From there, it was easy to set the leftBarButtonItem
, rightBarButtonItem
, and titleView
properties of my navigation controller's navigationItem
property to achieve custom buttons and titles. This works with the built-in navigation controller/navigation bar/navigation item logic, so there's no risk of things (like bar buttons) being hidden or obscured.
In a UINavigationBar
category:
- ( void )drawRect:( CGRect )rect
{
[ [ UIImage imageNamed:@"background-image.png" ] drawInRect:CGRectMake( 0, 0,
self.frame.size.width, self.frame.size.height ) ];
}
Right before calling pushViewController:animated:
, do something like this:
UIButton * leftButton = [ UIButton buttonWithType:UIButtonTypeCustom ];
leftButton.frame = CGRectMake( 0.0, 0.0, 65.0, 32.0 );
[ leftButton setImage:[ UIImage imageNamed:@"left.png" ] forState:UIControlStateNormal ];
[ leftButton addTarget:self action:@selector( leftAction ) forControlEvents:UIControlEventTouchUpInside ];
UIButton * rightButton = [ UIButton buttonWithType:UIButtonTypeCustom ];
rightButton.frame = CGRectMake( 0.0, 0.0, 65.0, 32.0 );
[ rightButton setImage:[ UIImage imageNamed:@"right.png" ] forState:UIControlStateNormal ];
[ rightButton addTarget:self action:@selector( rightAction ) forControlEvents:UIControlEventTouchUpInside ];
UIImageView * titleView = [ [ UIImageView alloc ] initWithImage:[ UIImage imageNamed:@"title.png" ] ];
UIBarButtonItem * leftBarButtonItem = [ [ UIBarButtonItem alloc ] initWithCustomView :leftButton ];
UIBarButtonItem * rightBarButtonItem = [ [ UIBarButtonItem alloc ] initWithCustomView:rightButton ];
nextController.navigationItem.leftBarButtonItem = leftButtonItem;
nextController.navigationItem.rightBarButtonItem = rightButtonItem;
nextController.navigationItem.titleView = titleView;
It's a little busy perhaps, but you could abstract away all this code into a method (or series thereof) somewhere else. This approach tends to work well if you want to completely override any of the styling that occurs in your navigation bar.
Hope this helps!