Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom background for UINavigationBar problems

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

like image 364
Michael Waterfall Avatar asked Jun 22 '26 02:06

Michael Waterfall


1 Answers

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
like image 73
Hauke Avatar answered Jun 23 '26 18:06

Hauke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!