6

since I updated to xcode 4.2 and iOS 5 following code has no effect:

@implementation UINavigationBar (CustomNavBarBG)
-(void)drawRect:(CGRect)rect{
    UIImage *image = [UIImage imageNamed:@"navbar.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.tintColor = [UIColor colorWithRed:124/255.0 green:177/255.0 blue:55/255.0 alpha:1.0];
}
@end

@implementation MyAppDelegate
//....

This should apply a background image and color to the navigation bar. But since the update I get the default blue color. I use the three20 framework in my project.

Any idea why this does not work? How can I set the background image for all navigation bars at one place and not in every view controller?

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • What about the update problems? http://stackoverflow.com/questions/15024037/updating-navigation-bar-after-a-change-using-uiappearance – Claus Feb 22 '13 at 12:28

5 Answers5

28

Try setting:

[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];

in your - (void) applicationDidFinishLaunching:(UIApplication *)application


EDIT: Seems helpfull too:

[[UINavigationBar appearance] setTintColor:myColor]; 
Totumus Maximus
  • 7,543
  • 6
  • 45
  • 69
  • ok thanks that works, do you have an idea how I can set the tint color the same way? When I put setTintColor instead setBackgroundImage the method is not found... – DarkLeafyGreen Oct 28 '11 at 13:41
  • The matter effect I do! [[UINavigationBar appearance] setTintColor:myColor]; – Totumus Maximus Oct 28 '11 at 13:53
  • actually this is what I do "[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:124/255.0 green:177/255.0 blue:55/255.0 alpha:1.0]];" but the back buttons in the toolbar are still blue – DarkLeafyGreen Oct 29 '11 at 12:07
  • for the back button you need a different setting again:D ill look it up for you later. – Totumus Maximus Oct 30 '11 at 01:05
2

There are native methods in ios5 to change the background image. I created a category for this that works on ios 4 & 5. (Dont have the code here atm) but should't be hard to create one.

[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Rolf Koenders
  • 439
  • 3
  • 8
1

I had the same issue of [[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault]; which doesn't work on iOS 4 but was working fine on iOS 5. So I tried an alternate solution for it.

image = [UIImage imageNamed:@"yourImageName.png"];
    if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
    {
        [[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }
    else
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        [self.navigationController.navigationBar addSubview:imageView];
    }
Titouan de Bailleul
  • 12,920
  • 11
  • 66
  • 121
Vishnu Gupta
  • 266
  • 2
  • 17
1

I have another solution, because the solution above hiding all buttons on navigation bar

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
Anton
  • 3,102
  • 2
  • 28
  • 47
1

If you already have an image set on your UINavigationBar and you would like to change it while your app is running then do this.

UIImage *NavigationPortraitBackground = [UIImage imageNamed:@"red_bg.png"];
[[UINavigationBar appearance] setBackgroundImage:NavigationPortraitBackground forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"red_bg.png"] forBarMetrics:UIBarMetricsDefault];
Sam B
  • 27,273
  • 15
  • 84
  • 121