Iphone – Setting UIToolbar background image with iOS5.1

background-imageios5.1iphoneuitoolbar

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

Best Solution

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