Why does -[[UIButton appearance] setBackgroundImag

2019-03-27 19:50发布

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?

1条回答
霸刀☆藐视天下
2楼-- · 2019-03-27 20:16

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];
查看更多
登录 后发表回答