4

I used these suggested solutions to give my UINavigationBar a custom image. (To make it more explicit: I added a category to UINavigationBar in my AppDelegate.m file). This worked fine so far and I didn't have any problems. Now however, I was running my app on the recent iOS5 beta. The UINavigationBar is blank now again.

Since all the other apps I have installed, that use a custom image, still behave the same there must be something "wrong" in my code I guess, that iOS5 now doesn't support anymore.

So does anybody have an idea what could be the problem with my adoption of the above mentioned solutions?

The only way I found to make it work was to create a real subclass of UINavigationBar and then in all the views tell IB to use that custom class. Not as elegant, though...

Community
  • 1
  • 1
Dennis
  • 2,223
  • 3
  • 27
  • 36
  • did you ever find the solution you were looking for? I understand that iOS5 needs to use setBackgroundImage:forBarMetrics:, but are you able to still use that in the Category sitting in your AppDelegate? All the solutions I'm finding are requiring me to add something to every ViewController just to support iOS5, which is what I was trying to avoid in the first place. – djibouti33 Oct 19 '11 at 06:00
  • From all the responses here so far it doesn't seem that there is an only-one-place-solution like it was prior-iOS 5. So my take is that the only way to achieve that would be to subclass all your ViewControllers not from UIViewController directly, but from your own one that simply implements the setBackgroundImage:forBarMetrics: by default... – Dennis Oct 19 '11 at 13:58
  • Take a look at here to use the new iOS5 API for UINavigationBar's [setBackgroundImage:forBarMetrics:](http://stackoverflow.com/questions/6283534/custom-background-image-on-uitoolbar-in-ios5-sdk/6343732#6343732) – loretoparisi Jun 14 '11 at 13:00

4 Answers4

4

To support both versions, you need to ask if there is setBackgroundImage:forBarMetrics: method.

Take a look at my blogpost about it: http://www.mladjanantic.com/setting-custom-background-for-uinavigationbar-what-will-work-on-ios5-and-ios4-too/

dormitkon
  • 2,526
  • 4
  • 39
  • 60
1

Use this code

        UIImage *backgroundImage = [UIImage imageNamed:@"strip.png"];
        [upnavbar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];

this wil work.

Nithinbemitk
  • 2,710
  • 4
  • 24
  • 27
  • 1
    If you can post the identical answer to two questions please consider flagging them as duplicates instead. – ChrisF Mar 16 '13 at 11:09
1

One more possible "hacky" solution is to create a custom view and insert it to UINavigationBar as a subView - may be it will still work:

UIView *backgroundView = ...
[navigationBar insertSubview:backgroundView atIndex:0];

Or check updated UINavigationBar class reference in iOS5 docs for built-in method to set custom background

Vladimir
  • 170,431
  • 36
  • 387
  • 313
0

This worked for me on my toolBar

//toolBar background image set based on iOS version
    [[UIDevice currentDevice] systemVersion];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {

        //iOS 5
        UIImage *toolBarIMG = [UIImage imageNamed: @"toolBar_brown.png"];  

        if ([toolBar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) { 
            [toolBar setBackgroundImage:toolBarIMG forToolbarPosition:0 barMetrics:0]; 
        }

    } else {

        //iOS 4
        [toolBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolBar_brown.png"]] autorelease] atIndex:0]; 

    }
RyeMAC3
  • 1,023
  • 1
  • 10
  • 17