使用iOS5.1设置UIToolbar背景图像

时间:2012-04-26 10:58:15

标签: iphone background-image uitoolbar ios5.1

我使用以下代码将图像“Sample”设置为我的UIToolbar的背景颜色

self.toolbar.layer.contents = (id)[UIImage imageNamed:@"Sample.png"].CGImage;

但似乎上面的内容不适用于iOS5.1,并且出现了UIToolbar的默认背景颜色。我对iOS4中的这一行没有任何问题。

我错过了什么吗?请建议我原因..

由于

3 个答案:

答案 0 :(得分:7)

正确的方法,它适用于app委托(与上面的解决方案不同),并且在头文件中也标有UI_APPEARANCE_SELECTOR,是:

[[UIToolbar appearance] setBackgroundImage:bgImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:bgImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsLandscapePhone];

答案 1 :(得分:4)

enter image description here

// Create resizable images
UIImage *gradientImage44 = [[UIImage imageNamed:@"imageName.png"] 
                            resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set the background image for *all* UINavigationBars
[[UIToolbar appearance] setBackgroundImage:gradientImage44 
                                   forBarMetrics:UIBarMetricsDefault];

最好使用iOS 5中引入的外观API。这可以帮助您

答案 2 :(得分:1)

setBackgroundImage:forToolbarPosition:barMetrics:这用于设置图像以用于给定位置和给定指标的背景。

在你的情况下

// Set the background image for PortraitMode
[self.toolbar setBackgroundImage:[UIImage imageNamed:@"Sample.png"] 
                                   forBarMetrics:UIBarMetricsDefault];
// and For LandscapeMode
[self.toolbar setBackgroundImage:[UIImage imageNamed:@"Sample.png"] 
                                   forBarMetrics:UIBarMetricsLandscapePhone];