Custom background for UINavigationBar issues
I was able to add a custom background to my navigation bar using:
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UINavigationBar.png"]];
[myViewController.navigationBar insertSubview:iv atIndex:0];
[iv release];
This works great and I can see the title and buttons are fine. However, when I go one level and then go back to the root, the buttons are hidden, but the title is still visible. The navbar image seems to cover the button elements.
I'm confused when I paste it at the bottom, so I would assume that the navigation items move and pop, that they appear above other views in the navigation bar.
Any ideas?
Thank,
Mike
source to share
Following the description here , I suggest adding a category to the navigation 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
source to share
This won't work on iOS 5, but the good news is there is an easy way to do it.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"name"] forBarMetrics:UIBarMetricsDefault];
NOTE. This will only work on iOS 5 and up, so make sure you check the iOS version if you want to be backward compatible.
source to share
You can try adding this to viewDidAppear (in every view manager that manages navigation / :):
NSLog(@"navbar subviews before \n %@",[[[self navigationController] navigationBar] subviews]);
for (UIImageView *imgv in [[[self navigationController] navigationBar] subviews]) {
[[[self navigationController] navigationBar] sendSubviewToBack:imgv];
}
NSLog(@"navbar subviews after \n %@",[[[self navigationController] navigationBar] subviews]);
I used Hauek's solution , but when the bar tastes differently in other controllers, it might give some minor problems, not the best option anyway.
Hope it helps to at least understand why what you were doing is not working,
source to share
Instead of doing it in drawRect:, you can override the drawLayer: inContext: method in the UINavigationBar category class. Inside the drawLayer: inContext: method, you can draw the background image you want to use. Also you can choose different images for portrait and landscape orientation if your application supports multiple interface orientations.
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
if ([self isMemberOfClass:[UINavigationBar class]] == NO) {
return;
}
UIImage *image = (self.frame.size.width > 320) ?
[UINavigationBar bgImageLandscape] : [UINavigationBar bgImagePortrait];
CGContextClip(context);
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
}
This complete Xcode demo project on customizing the appearance of a UINavigationBar might also be helpful.
source to share
Now you can just add a new background to your UINavigationBar.
/////////
UINavigationController *navController = [[UINavigationController alloc] init];
UINavigationBar*bar=[navController navigationBar]; // get navigation bar
NSArray*barSubview=[bar subviews]; // get all subviews of bar
UIView*backView=[arr objectAtIndex:0]; // at index 0 lying background view of our bar
backView.alpha=0.0f;// set alpha to 0 and it will become invisible
UIImageView*imgView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top-bar.png"]];// create the image view that we will insers as subbview in our bar
imgView.frame=CGRectMake(0, 0, 320, 44);
[bar insertSubview:imgView atIndex:0];// and it will be out new background
[imgView release];
source to share