Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize NavigationBar in iOS 5

In iOs4 i created a custom navigation bar using this snippet

#import "UINavigationBar+CustomImage.h"


@implementation UINavigationBar (CustomImage)

- (void)drawRect:(CGRect)rect {
    // Drawing code
    UIImage *image = [[UIImage imageNamed:@"header.png"] retain];
    [image drawInRect:CGRectMake(0, 0,self.frame.size.width , self.frame.size.height)];
    [image release];
    }
@end

and it is well for ios4 in my app. Now I want to run this in iOs5 and the problem is that the custom navigation bar doesn't appear the way I want.

Can any one help me to create a custom navigation bar for iOs5.

like image 475
Harin Avatar asked Jan 18 '23 12:01

Harin


2 Answers

You need to use the appearance proxy. But, make sure to check if respondsToSelector for iOS4. Leave your current method in place for iOS4 and it will work on both.

// not supported on iOS4
UINavigationBar *navBar = [purchaseNavController navigationBar];
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
    // set globablly for all UINavBars
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"brnlthr_nav.jpg"] forBarMetrics:UIBarMetricsDefault];

    // could optionally set for just this navBar
    //[navBar setBackgroundImage:...
}
like image 163
bryanmac Avatar answered Jan 21 '23 00:01

bryanmac


The same problem is mentioned here: UINavigationBar's drawRect is not called in iOS 5.0. Please check this may help you.

like image 26
UPT Avatar answered Jan 21 '23 00:01

UPT