views:

973

answers:

2

I am looking for a way to have a custom navigation bar and need to have a custom navigation bar background to achieve this. I was looking around for how to do this, but could not find a solution. If anyone has the solution, help is much appreciated.

+1  A: 

You can just add a subview (a UIImageView) to the navaigationBar, which is just a UIView subclass.

UINavigationBar nb = [[UINavigationBar alloc]init];
[nb addSubview: foo];
[nb release];

Here's a forum post that describes how to wrap this up into a category: http://discussions.apple.com/thread.jspa?threadID=1649012&tstart=0

Andrew Johnson
A: 

(supplemental to Andrew Johnson's response)

The linked Apple.com post includes 3 or 4 different solutions, most of which only "half" work. I think the most elegant/effective of them is this one:

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

HOWEVER ... it's not good-practice ObjC to od that as a category (should be an override), and it has some problems of its own.

So, a more general and powerful solution is here:

http://samsoff.es/posts/customize-uikit-with-method-swizzling

Adam
Thanks for linking to me Adam :) You're right that overriding methods with categories is a very bad practice.
Sam Soffes