I want to add an image background to my navigation bar.
Is it right?
//set custom background image
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavigationBackground.png"]];
[self.navigationBar insertSubview:backgroundView atIndex:0];
[backgroundView release];
Your code alone won't do it, you'll have to write a category in order for it to work. There are two approaches regarding the way you should do it: the first one involves making the image a subview of your
UINavigationBar
and re-bringing it to front in each of yourUIViewController
'sviewDidAppear
method. This however has been reported having some issues with covering the rightUIBarButtonItem
. The other method involves overriding- (void)drawRect:(CGRect)rect
and drawing the image there. Both of these are extensively covered in this blog discussion..
I would do this in the appdelegate something like
And in
.h file:
what i did was just created an UIImage with the image i wanted and added to the navigation bar like this.
While adding Navigation Bar background image, you should be very careful about its dimensions. Please make sure you have following dimensions 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 following code to add background image and to avoid any tiling in Navigation Bar's background image.
Here's the code from the link @luvieere mentioned. Paste this code into to the rootview controller just above
@implementation rootviewController
As of iOS 5, there is an official way to do this. (see iOS Developer Library)
But still, retain the old code for backward compatibility unless you really want to ditch iOS 4 and below.
Swift version :
Updated Swift 3.2 :