0

In my universal app,

I am setting an image on navigation bar...using **

objective c category...on UINavigationBar

**

The code works fine in ios 5.0 in iphone *The code works fine in ios 4.3 in iPhone/iPad*

*But not working in **ios 5.0 iPad***





- (void) drawRect:(CGRect)rect 
    {
    UIImage *image;

   image = [UIImage imageNamed: @"Navigation.png"];

    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];  

      [self setTintColor:[UIColor clearColor]];

      NSLog(@"Draw Rect");

    }
Arpit B Parekh
  • 1,932
  • 5
  • 36
  • 57

2 Answers2

6

To set a BG image for navigation bar in iOS 5 you can use the below code

if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Navigation.png"] forBarMetrics:UIBarMetricsDefault];
}

But please keep in mind that this wont work in iOS 4. To make it work in both you also need to add a category to UINavigationBar

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed:@"Navigation.png"];
[img drawInRect:rect];
}
sElanthiraiyan
  • 6,000
  • 1
  • 31
  • 38
  • You dont have to find the iOs version if you use both code snippets. If u still want to find http://stackoverflow.com/questions/820142/how-to-target-a-specific-iphone-version – sElanthiraiyan Jan 17 '12 at 13:41
2

If you want to set the background application-wide you can use iOS5's UIAppearance.

[[UINavigationBar appearance] setBackgroundImage:X forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:X forBarMetrics:UIBarMetricsLandscape];
Brandon Cordell
  • 1,308
  • 1
  • 9
  • 24