How to add background image to iphone navigation bar?

I want to add an image background to my navigation bar

Is it correct?

//set custom background image UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavigationBackground.png"]]; [self.navigationBar insertSubview:backgroundView atIndex:0]; [backgroundView release]; 
+32
iphone cocoa-touch background uinavigationbar
Nov 07 '09 at 8:59
source share
12 answers

Here is the code from. Paste this code into the rootview controller above @implementation rootviewController

 @implementation UINavigationBar (CustomImage) - (void)drawRect:(CGRect)rect { UIImage *image = [UIImage imageNamed:@"NavigationBar.png"]; [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; } @end 

Like iOS 5, there is an official way to do this. (see iOS Developer Library )

 // someplace where you create the UINavigationController if ([navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) { UIImage *image = [UIImage imageNamed:@"NavigationBar.png"]; [navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; } 

But still keep the old code for backward compatibility if you really don't want to cut iOS 4 and below.

+98
Dec 04 '09 at 14:18
source share

Only your code will not do this, you will need to write a category for it to work. There are two approaches to how you should do this: the first involves creating an image subtype of your UINavigationBar and re-drawing it forward in each of your UIViewController viewDidAppear methods. However, some UIBarButtonItem rights coverage issues have been UIBarButtonItem . Another method involves overriding

- (void)drawRect:(CGRect)rect

and draw an image there. Both are described in detail in this blog .

+32
Nov 07 '09 at 11:56
source share

The easiest way is to simply set the contents of the UINavigationBar layer.

 NSString *barBgPath = [[NSBundle mainBundle] pathForResource:@"mybgimage" ofType:@"png"]; [nBar.layer setContents: (id)[UIImage imageWithContentsOfFile: barBgPath].CGImage]; 

The underside is buttons that are not generated using the correct hue, but you can set the color of the navigation bar to what is closest to your bg image, and you should take care of the hue.

+9
Aug 31 '10 at 19:08
source share

I would do it in appdelegate something like

 + (void)Generalstyle { //navigationbar UINavigationBar *navigationBar = [UINavigationBar appearance]; [navigationBar setBackgroundImage:[UIImage imageNamed:@"navigation.png"] forBarMetrics:UIBarMetricsDefault]; } 

And in

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[self class] Generalstyle]; } 

.h file:

 + (void) Generalstyle; 
+8
Jul 31 '12 at 15:51
source share

in ios5, I set the background image of the navigation bar (for the entire image of the navigation bar) in AppDelegate.m :

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [application setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO]; UIImage *navBarBackgroundImage = [UIImage imageNamed:@"nav_bg"]; [[UINavigationBar appearance] setBackgroundImage:navBarBackgroundImage forBarMetrics:UIBarMetricsDefault]; return YES; } 
+8
Aug 08 '13 at 3:26
source share

When iphone 5 arrives, we need to install both types of device. Therefore use this

 if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) { //iOS 5 new UINavigationBar custom background [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbg_ForiPhone5_Imagename.png"] forBarMetrics: UIBarMetricsDefault]; } else { [self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbg_ForOtherIphone_Imagename.png"]] atIndex:0]; } 

For more information, click here

+2
Dec 19 '12 at 12:38
source share

This works great on iOS 6 and 7.

 UINavigationBar *navBar = [[self navigationController] navigationBar]; UIImage *backgroundImage = [UIImage imageNamed:@"nav-bar-background-normal"]; [navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault]; 
+2
Feb 12 '14 at 12:09
source share

You can also add a category that extends the UINavigationBar class and overrides the drawRect:> method.

0
Nov 07 '09 at 9:08
source share

what I did was just create a UIImage with the image I wanted and added it to the navigation bar like this.

 UIImage *image = [UIImage imageNamed:@"yourImage.png"]; [[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 
0
Sep 04 '13 at 19:54 on
source share

Quick version :

 let navBar = UINavigationBar.appearance(); navBar.setBackgroundImage(UIImage(named: "yourImageName"), forBarMetrics: .Default); 
0
Apr 12 '16 at 20:34
source share

When adding a background image to the navigation bar, you have to be very careful in your measurements. Please make sure that you have the following sizes for different screen sizes.

1) background.png => 320x44
2) background@2x.png => 640x88 // used for iPhone5 and for retina devices
3) background@3x.png => 1334x183 // used for iPhone6

Use the following code to add a background image and to prevent the background image from alternating in the navigation bar.

 [self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:@"background"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault]; 
0
Feb 27 '17 at 6:52
source share

Try using this code in the AppDelegate class to display an image in the navigation bar. This will help you a lot.

 [[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"headerImg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forBarMetrics:UIBarMetricsDefault]; 
0
Mar 30 '17 at 17:02
source share



All Articles