Thursday, 26 April 2012

Navigation - bar set background image for both ios5 & ios 4

In ios > 5 API updated with barMetrics arguments to setbackgroundimage for UInavigationbar and in ios < 5 only & only option to set background-image on navigationbar by calling drawRect () function  but it didn't work for me , resulting some errors on this.After being a lot of search on this but i'm totally failed.So,I did some trick on this and it works for me.

if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
For ios 5 or above

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"YOUR_IMAGE_NAME_WITH_EXTENSION"] forBarMetrics:UIBarMetricsDefault];
}
else {

 For ios 4 or less 

NSString *barBgPath = [[NSBundle mainBundle] pathForResource:@"YOUR_IMAGE_NAME" ofType:@"YOUR_IMAGE_EXTENSION"];

[self.navigationController.navigationBar.layer setContents:(id)[UIImage imageWithContentsOfFile: barBgPath].CGImage];

self.navigationController.navigationBar.alpha=1.0; //If you want change alpha

}

Cheers:)
Enjoyyyyyy!

7 comments:

  1. Thanks Help me A lot.
    Words like charm. :)

    ReplyDelete
  2. Hi i'm using this code in the viewDidLoad functions of my views, and on the iphone simulator(ios 6.1) it works perfectly. On the otherhand when i build my app for my iphone 3gs(ios4.3) i only see the navbar backkground image for the rootviewcontroller. Have you got any ideas? Thanks

    ReplyDelete
  3. The only way i could get everything working(ios6.1 simulator and ios4.3 iphone 3gs) was to add:

    @implementation UINavigationBar (CustomImage)

    - (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"navbar_bg.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }

    @end

    in my viewController .m files to make it work for ios <=4
    and add:
    if ([navControllerName.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    [navControllerName.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg.png"] forBarMetrics:UIBarMetricsDefault];
    }
    in my AppDelegate.m for each navcontroller.


    Is there a way of adapting your code to work on all of my viewcontrollers in my 3gs? As i would like to avoid overriding the 'drawRect:' function.

    Thank you

    ReplyDelete
    Replies
    1. Hi Pollaci,
      Please don't use category.
      You here need to subclassing of Navigation Bar. So, you need to assign your subclass name to your view controllers.
      Let say,

      @interface MyNavigationBar : UINavigationBar


      @implementation MyNavigationBar



      - (void)drawRect:(CGRect)rect {
      UIImage *backgroundImage = [UIImage imageNamed:@"navbar_bg.png"];
      CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(0, 0, self.frame.size.width,
      self.frame.size.height), backgroundImage.CGImage);
      }

      Always remember you need to assign this MyNavigationBar class name to navigationBar to your associated view controller. Also, iOS checks to be implemented as I suggested above.

      Hope this works :)
      Thanks

      Delete