开发者

UINavigationBar custom background image

开发者 https://www.devze.com 2023-03-12 13:40 出处:网络
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).

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 be开发者_StackOverflowta. 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...


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/


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


Use this code

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

this wil work.


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

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消