Search code examples
objective-cthree20uinavigationbarxcode4.2

Set background image of navigation bar for all bars


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?


Solution

  • Try setting:

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

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


    EDIT: Seems helpfull too:

    [[UINavigationBar appearance] setTintColor:myColor];