Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Custom background for UINavigationBar?

Hi,
how to set a custom background image for the whole navigation bar of a navigation controller? I can't find any appropriate API method.
I tried already to mess around with the views of the UINavigationBar to insert my background view - with the result no not-showing buttons and titles :-/
Also subclassing of UINavigationBar with a new drawRect method does not lead to anywhere - there is simply now way to set the navigation bar for a UINavigationController!
Any help would be highly appreciated!
Best regards,
Matthias Huber

MacBook, Mac OS X (10.5.4), iPhone OS 2.0

Posted on Aug 5, 2008 3:05 AM

Reply
27 replies

Aug 24, 2008 1:11 PM in response to hubermat

Create a category that extends UINavigationBar:

@implementation UINavigationBar (UINavigationBarCategory)
-(void)setBackgroundImage:(UIImage*)image{
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);
[self addSubview:aTabBarBackground];
[self sendSubviewToBack:aTabBarBackground];
[aTabBarBackground release];
}
@end


And call this method from wherever you are initializing your UINavigationController:


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
// Create the navigation and view controllers
SinglePlayerRootViewController *_rootViewController = [[SinglePlayerRootViewController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *_navigationController = [[UINavigationController alloc] initWithRootViewController:_rootViewController];
self.navigationController = _navigationController;
[_navigationController release];
[_rootViewController release];
// set the image for the top navigation bar
[[navigationController navigationBar] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackgroundTop.png"]];
// Configure and show the window
[self.view addSubview:navigationController.view];
}
return self;
}


I hope this helps!
-ML

Aug 25, 2008 10:59 AM in response to mlara

I was doing this a similar way and your method appears to have the same flaw. This works for the first screen of the navigation controller but as soon as you go to a new view controller the title then appears behind the image that's been set as background. I guess the navigation controller must add the new navigation item at index 0 which is where the background image is.

I've not found a way around this yet, but there are apps out there doing it so... help!

Oct 20, 2008 1:29 AM in response to eknathkadam

Not sure if this addition is what you were hoping for, but I'm using this:

/* input: The image and a tag to later identify the view */
@implementation UINavigationBar (UINavigationBarCategory)
-(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]];
}
@end

You will probably do well with the resetBackground called in each viewcontroller's viewDidAppear like so:

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[self.navigationController navigationBar] resetBackground:8765309];
}


The 8765309 is the tag number for this example, it can be anything you like.
It must be the same number you use when setting the background tag.

[[self.navigationController navigationBar] setBackgroundImage:myBackgroundImage withTag:8675309];

Apr 1, 2009 1:57 AM in response to Branded3

So I figured it out, so here's some info for the rest of you newbs.

I put the following piece of code in my AppDelegate.m file above my @implementation AppDelegate line (still dont know what UINavigationBarCategory is for)

/* input: The image and a tag to later identify the view */
@implementation UINavigationBar (UINavigationBarCategory)
-(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]];
}
@end

Then placed the following code in my applicationDidFinishLaunching

RootViewController *_rootViewController = [[RootViewController alloc] init];
UINavigationController *_navigationController = [[UINavigationController alloc] initWithRootViewController:_rootViewController];
_navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
self.navigationController = _navigationController;
[_navigationController release];
[_rootViewController release];

Apr 29, 2009 7:49 AM in response to Branded3

(still dont know what UINavigationBarCategory is for)

Categories are typically used to extend or partially override the functionality of existing classes without needing to a) subclass them or b) replace the classes with an entire different, custom class.

So someone else could create a MyAWESOMEUINavigationBarCategory (naming conventions notwithstanding) and include it in their project to achieve an entirely different set of functionality without having to change their code at all. It's really slick.

May 10, 2009 1:38 PM in response to hubermat

Try overriding - (void)drawRect:(CGRect)rect like so:

@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


This works well for me, and doesn't appear to have issues with pushing new view controllers, hidden titles and bar button items, etc. I'm still fairly new at this, though 🙂

Custom background for UINavigationBar?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.