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

99封情书 提交于 2019-12-04 20:53:18

问题


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


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?


回答1:


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


来源:https://stackoverflow.com/questions/8734419/why-does-uibutton-appearance-setbackgroundimage-affect-the-initial-appearan

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