Setting UIToolbar background image with iOS5.1

半世苍凉 提交于 2019-12-24 03:28:25

问题


I used the following code to set the image "Sample" as a background color of my UIToolbar

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

But it seems the above one does not works with iOS5.1 and the default background color of UIToolbar appeared. I doesn't have any problem with this line in iOS4.

Am I missing something? Please suggest me the reason..

Thanks


回答1:


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

Better to use appearance API introduced with iOS 5. May this will help you




回答2:


The correct way, which works from the app delegate (unlike the solution above) and which is also marked with UI_APPEARANCE_SELECTOR in the header files, is:

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



回答3:


setBackgroundImage:forToolbarPosition:barMetrics: this is used to set the image to use for the background in a given position and with given metrics.,

In your case

// 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/setting-uitoolbar-background-image-with-ios5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!