Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does -[[UIButton appearance] setBackgroundImage] affect the initial appearance of UIBarItem objects and how do you correct it?

When customizing the appearance of UIButton using the class proxy UIBarItems seem to initially take on the custom properties set for UIButton.

Starting with the default Master/Detail project using Core Data. Customize the appearance of UIButton in the AppDelegate and run the app. Click the Edit button, then the Done button in the navigation bar for the MasterViewController and watch the customization go away.

Custom appearance code in [AppDelegate application:didFinishLaunchingWithOptions]:

UIImage *customBackground = [[UIImage imageNamed:@"yourcustomimage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5,5,5,5)];  
[[UIButton appearance] setBackgroundImage:customBackground forState:UIControlStateNormal];  

All UIBarButtonItems initialize with custom background.
All UIBarButtonItems initialize with custom background

When the Edit button is replaced by the Done button, it correctly does not have the customized background.
When the Edit button is replaced by the Done button, it correctly does not have the customized background.

A similar question asks how to customize the Done button. I'm concerned why this is happening at all to UIBarItem objects, which do not inherit from UIButton, and would like to know how to correct it.

I suspect the proxy inheritance and the supported properties, but I don't know how to correct for it. Any suggestions?

like image 459
sean woodward Avatar asked Jan 04 '12 21:01

sean woodward


1 Answers

My suggestion would be to reset backgroundImage to nil when contained in UINavigationBar:

[[UIButton appearance] setBackgroundImage:customBackground forState:UIControlStateNormal];
[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setBackgroundImage:nil forState:UIControlStateNormal];
like image 96
proxi Avatar answered Oct 20 '22 22:10

proxi