views:

1057

answers:

1

Hi there,

I've managed to add a custom background to my navigation bar by using:

UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UINavigationBar.png"]];
[myViewController.navigationBar insertSubview:iv atIndex:0];
[iv release];

This works fine and I can see the title and buttons okay. However, when I drill down one level and then go back to the root, the buttons are hidden, but the title is still visible. It appears the navigation bar image is covering the button items.

I'm confused as I'm inserting it at the bottom so I would assume when the navigation items are pushed and popped that they are displayed above other views in the navigation bar.

Any ideas?

Thanks,

Mike

+11  A: 

Following the description from here, I suggest adding a category to the nav bar.

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
    // Drawing code 
    UIImage *img = [UIImage imageNamed: @"navbar_background.png"];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, CGRectMake(0, 0, 320, self.frame.size.height), img.CGImage);

}
@end
Hauke
Thanks for that! It's working except that it appears the navigation bar is adding a dark to light gradient over the top of the image! Is there any way of removing that effect?
Michael Waterfall
Actually, it appears it is drawing the PNG upside down!? Any ideas?
Michael Waterfall
Ah got it... you need to use [img drawInRect:CGRectMake(0, 0, 320, self.frame.size.height)]; instead of CGContextDrawImage()!
Michael Waterfall
You can add the above code to the bottom of your appdelegate.m file. Just mentioning this because I didn't get this without some googling.
Les
@Michael Waterfall [img drawInRect:rect];
Zaki