4

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.

midhunhk
  • 5,560
  • 7
  • 52
  • 83
Harin
  • 867
  • 11
  • 23

4 Answers4

4

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:...
}
bryanmac
  • 38,941
  • 11
  • 91
  • 99
3

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

Community
  • 1
  • 1
UPT
  • 1,490
  • 9
  • 25
2

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];
Siem Abera
  • 824
  • 8
  • 9
0

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]; }

Senthil
  • 510
  • 12
  • 28