我使用以下代码将图像"Sample“设置为我的UIToolbar的背景色
self.toolbar.layer.contents = (id)[UIImage imageNamed:@"Sample.png"].CGImage;但上面的版本似乎不适用于iOS5.1,并且出现了默认的背景颜色UIToolbar。在iOS4中,我对这一行没有任何问题。
我是不是遗漏了什么?请告诉我原因。
谢谢
发布于 2012-04-26 19:21:11

// 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引入的外观接口,希望这会对你有所帮助
发布于 2013-02-07 12:38:42
正确的方法是从应用程序委托(与上面的解决方案不同)开始,并且在头文件中也标记为UI_APPEARANCE_SELECTOR:
[[UIToolbar appearance] setBackgroundImage:bgImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:bgImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsLandscapePhone];发布于 2012-04-26 19:18:07
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];https://stackoverflow.com/questions/10332040
复制相似问题