views:

2142

answers:

3

how do i show a background image on a navigation bar or give tint color to the navigation bar in a native iphone application??

+1  A: 

a background image is going to take a bit more work (you might want to try setting a titleView that's the same size as the bar itself; I haven't tried this myself) or adding a view behind existing subviews. Tint color is easy: navBar.tintColor = [UIColor orangeColor];

Ben Gottlieb
+1  A: 

Was looking for this a week ago. Found this over here discussions. apple. com/thread.jspa?threadID=1649012&tstart=0 (sorry won't let me post a real link).

-(void)setBackgroundImage:(UIImage*)image withTag:(NSInteger)bgTag{
if(image == NULL){ //might be called with NULL argument
 return;
}
UIImageView *aTabBarBackground = [[UIImageView alloc]initWithImage:image];
aTabBarBackground.frame = CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
aTabBarBackground.tag = bgTag;
[self addSubview:aTabBarBackground];
[self sendSubviewToBack:aTabBarBackground];
[aTabBarBackground release];
}
/* input: The tag you chose to identify the view */
-(void)resetBackground:(NSInteger)bgTag {
    [self sendSubviewToBack:[self viewWithTag:bgTag]];
}

I made this as a category to UINavigationBar. To set it a background image for a UINavigationBar inside a UINavigationBarController, I did this:

[navigationControllerForChannels.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_bar.png"] withTag:48151623];

I've had some buginess when updating the tab bar, so you'll want to call

[self.navigationController.navigationBar resetBackground:48151623];

After any modifications to the bar.

dbachrach
A: 

This's how I did on iOS4:

#import <QuartzCore/QuartzCore.h> // For .layer 

self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"navigationBarBackgroundImage"].CGImage;
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];

No need to switch subviews between z-orders (-exchangeSubviewAtIndex:withSubviewAtIndex:), both background image and tintColor set in one line of code, and works with @2x image too.

digdog