Using image color or hue on uinavigationbar in iphone?

How do I show the background image in the navigation bar or colorize the navigation bar in my own iphone application?

+6
iphone xcode interface-builder
source share
7 answers

Looking for it a week ago. We found discussions here. an Apple. com / thread.jspa? threadID = 1649012 & tstart = 0 (sorry, do not 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 it as a category for the UINavigationBar. To set this background image for the UINavigationBar inside the UINavigationBarController, I did this:

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

I had some scam when updating the tab bar so you want to call

 [self.navigationController.navigationBar resetBackground:48151623]; 

After any changes to the panel.

+6
source share

For iOS5, use the following lines of code:

 UINavigationBar *navBar = [[self navigationController] navigationBar]; UIImage *backgroundImage = [UIImage imageNamed:@"nav-bar-background-normal"]; [navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault]; 

For backward compatibility, check to see if the navigation bar responds to setBackgroundImage: forBarMetrics:

Further information on: http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/

+10
source share

This is how I did it on iOS4:

 #import <QuartzCore/QuartzCore.h> // For .layer self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"navigationBarBackgroundImage"].CGImage; self.navigationController.navigationBar.tintColor = [UIColor orangeColor]; 

There is no need to switch sub-selections between z-orders (-exchangeSubviewAtIndex: withSubviewAtIndex :), both the background image and tintColor installed on the same line of code work with the @ 2x image.

+8
source share

You can override the UINavigationBar drawRect. The code can be placed in appDelegate.m I tested it and it works with 3x and 4x iOS.

 @implementation UINavigationBar (UINavigationBarCategory) - (void)drawRect:(CGRect)rect { UIColor *color = [UIColor blackColor]; //tint color UIImage *img = [UIImage imageNamed: @"navBarBg.png"]; // your image [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; self.tintColor = color; }@end 
+3
source share

For iOS5 and iOS6, I used these solutions, and it worked perfectly, creating a universal background image of the UINavigationBar.

Retina Portrait iPhone 640px x 88px / iPhone Non Retina Portrait 320px x 44px

Inside AppDelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

Put this code

 // Set the status bar to black color. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO]; // Change @"menubar.png" to the file name of your image. UIImage *navBar = [UIImage imageNamed:@"menubar.png"]; [[UINavigationBar appearance] setBackgroundImage:navBar forBarMetrics:UIBarMetricsDefault]; 

Remember to change the image name (menubar.png)

Check out this link for a complete answer http://www.lwxted.com/blog/2012/add-custom-background-image-uinavigationbar-ios-5/

+2
source share

the background image will take a little more work (you might want to try setting a heading that will be the same size as the panel itself, I haven't tried it myself) or adding a view behind existing subviews. The hue color is simple: navBar.tintColor = [UIColor orangeColor];

+1
source share

If you use the CGImage solution, you may have a problem with the image size:

 CGRect layer=self.navigationController.navigationBar.frame; layer.size.height=57.0; layer.origin.y=0; self.navigationController.navigationBar.frame=layer; CGImageRef imageRef = [UIImage imageNamed:@"myImg.png"].CGImage; self.navigationController.navigationBar.layer.contents = (id)imageRef; 

It seems to me that the image is stretched down, since the layer apparently has a height of 44.0 pixels, but the background image for the UINavigationBar should be at least 57.0.

If you try to move the layer frame, all the buttons will move inside it.

0
source share

All Articles