2

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

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

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

我错过了什么吗?请告诉我原因..

谢谢

4

3 回答 3

7

从应用程序委托(与上面的解决方案不同)工作并且在头文件中也标有 UI_APPEARANCE_SELECTOR 的正确方法是:

[[UIToolbar appearance] setBackgroundImage:bgImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:bgImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsLandscapePhone];
于 2013-02-07T04:38:42.013 回答
4

在此处输入图像描述

// 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。希望这会对您有所帮助

于 2012-04-26T11:21:11.283 回答
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];
于 2012-04-26T11:18:07.427 回答