11

Possible Duplicate:
How to add background image on iphone Navigation bar?

I am looking for a way to have a custom navigation bar and need to have a custom navigation bar background to achieve this. I was looking around for how to do this, but could not find a solution. If anyone has the solution, help is much appreciated.

Community
  • 1
  • 1
intl
  • 2,753
  • 9
  • 45
  • 71
  • There more solutions alternative solutions here: http://stackoverflow.com/questions/1692487/how-to-add-background-image-on-iphone-navigation-bar – Leslie Godwin Feb 02 '12 at 08:02

4 Answers4

14

Since iOS5, you can easily set a custom background, with the method setBackgroundImage:forBarMetrics: But you must check if the user's phone has the right OS.

if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"texture.png"]
                                                  forBarMetrics:UIBarMetricsDefault];
}

This is a nicer solution, cause it's in the doc.

Martin
  • 11,881
  • 6
  • 64
  • 110
10

(supplemental to Andrew Johnson's response)

The linked Apple.com post includes 3 or 4 different solutions, most of which only "half" work. I think the most elegant/effective of them is this one:

@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

HOWEVER ... it's not good-practice ObjC to od that as a category (should be an override), and it has some problems of its own.

So, a more general and powerful solution is here:

http://samsoff.es/posts/customize-uikit-with-method-swizzling

Adam
  • 32,900
  • 16
  • 126
  • 153
  • Thanks for linking to me Adam :) You're right that overriding methods with categories is a very bad practice. – Sam Soffes Jul 13 '10 at 18:29
  • Someone downvoted this without explanation? Not sure why. Although I would add: this whole question (and most of the answers) are iOS 2/3/4 only - iOS 5 has a whole new system of doing this. The answer still works for the people that are doing iOS4. – Adam May 18 '12 at 16:02
6

You can just add a subview (a UIImageView) to the navaigationBar, which is just a UIView subclass.

UINavigationBar nb = [[UINavigationBar alloc]init];
[nb addSubview: foo];
[nb release];

Here's a forum post that describes how to wrap this up into a category: http://discussions.apple.com/thread.jspa?threadID=1649012&tstart=0

Andrew Johnson
  • 13,108
  • 13
  • 75
  • 116
3

Copy this into viewDidLoad. It will check for iOS 5 and use the preferred method, otherwise it will add a subview to the navBar for iOS versions < 5.0. This will work provided that your custom background image has no transparencies.

float version = [[[UIDevice currentDevice] systemVersion] floatValue];
NSLog(@"%f",version);
UIImage *backgroundImage = [UIImage imageNamed:@"myBackgroundImage.png"];
if (version >= 5.0) {
    [self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
}
else
{
    [self.navigationController.navigationBar insertSubview:[[[UIImageView alloc] initWithImage:backgroundImage] autorelease] atIndex:1];
}
chazzwozzer
  • 467
  • 6
  • 14