IOS5 SDK中UIToolbar上的自定义背景图像

Sta*_*his 27 uitoolbar ios5

昨天下载了IOS5 SDK,这个用于将我的UIToolbar背景设置为自定义图像的代码停止工作.如果我将目标设置为IOS4.3及以下,它仍然有效.

[self.bizToolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbar-iphone.png"]] autorelease] atIndex:0];
Run Code Online (Sandbox Code Playgroud)

有人在IOS 5上遇到过这个吗?

小智 49

您可以使用:( [[UIToolBar appearance] setBackgroundImage:toolBarIMG forBarMetrics:UIBarMetricsDefault];iOS 5中的新功能)

  • [[UIToolbar外观] setBackgroundImage:barsBack forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault] (59认同)

lor*_*isi 35

假设你链接了iOS5 beta SDK,你可以这样做

if([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        //iOS 5 new UINavigationBar custom background
        [navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
} 
Run Code Online (Sandbox Code Playgroud)

要实现这一点,请看一下iOS 4.3到iOS 5.0 API的差异 并搜索"UINavigationBar.h"

或者仔细看看这里的新方法签名 setBackgroundImage:forBarMetrics:

这里还有UIBarMetrics枚举类型

希望这可以帮助.


Rye*_*AC3 10

这适用于我的工具栏:

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

    }
Run Code Online (Sandbox Code Playgroud)

  • 没有必要检查系统版本(这通常不是一个好习惯),因为`-respondsToSelector`检查应该足以确定是使用新方法还是旧方法. (6认同)