Harin
Harin

Reputation: 867

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.

Upvotes: 4

Views: 8124

Answers (4)

Senthil
Senthil

Reputation: 510

Check the iOS version and set the navigation bar image

if([[[UIDevice currentDevice] systemVersion]floatValue] <5.0){ } else{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"Sample.png"] forBarMetrics:UIBarMetricsDefault]; }

Upvotes: 0

Siem Abera
Siem Abera

Reputation: 824

Just put the following code in the application delegate and it will work fine, Even in ios5
I put it in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


// Create image for navigation background - portrait

UIImage *NavigationPortraitBackground = [[UIImage imageNamed:@"NavigationPortraitBackground"] 
                                             resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

// Create image for navigation background - landscape

UIImage *NavigationLandscapeBackground = [[UIImage imageNamed:@"NavigationLandscapeBackground"] 
                                          resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];


// Set the background image all UINavigationBars

[[UINavigationBar appearance] setBackgroundImage:NavigationPortraitBackground 
                                   forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:NavigationLandscapeBackground 
                                   forBarMetrics:UIBarMetricsLandscapePhone];

Upvotes: 2

UPT
UPT

Reputation: 1480

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

Upvotes: 3

bryanmac
bryanmac

Reputation: 39296

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:...
}

Upvotes: 4

Related Questions