1

So I have a category for UINavigationController and I override the init method as below

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
            UIImage *image = [UIImage imageNamed:@"navBar.png"];
            [[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];      
    }

        return self;
}

The following code causes the background image of the navigation bar to become blank (white). The image name is correct and it exists within the project. Any idea what is causing this problem?

aryaxt
  • 76,198
  • 92
  • 293
  • 442

1 Answers1

4

If you use iOS4, you can override - (void)drawRect

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect
{
    UIImage *navBg = [UIImage imageNamed:@"navBar.png"];
    [navBg drawInRect:rect];
}
@end

iOS 5,

if ([[UIDevice currentDevice].systemVersion floatValue] >= 5.0) {
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){

        UIImage *navBarImg = [UIImage imageNamed:@"navBar.png"];

        [self.navigationController.navigationBar setBackgroundImage:navBarImg forBarMetrics:UIBarMetricsDefault];

    }
}
Tom van Zummeren
  • 9,130
  • 12
  • 52
  • 63
xda1001
  • 2,449
  • 16
  • 18
  • and this link may help you http://stackoverflow.com/questions/5272451/overriding-methods-using-categories-in-objective-c – xda1001 Feb 16 '12 at 04:42