如何设置UIButton的外观而不影响UIBarButtonItems?

Mac*_*wic 9 appearance objective-c ios ios5 uiappearance

使用以下代码自定义常规UIButtons还会影响文本字段中的UIBarButtonItems和清除按钮.

[[UIButton appearance] setBackgroundImage:greenButtonImage forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

希望可言,只有经常定制后者元素圆矩形按钮.我意识到使用appearanceWhenContainedIn:可用于为UIBarButtonItems和UITextField设置自定义外观,但我希望这些按钮保持标准.子类化不是一个选项,因为它不应该用于这样一个简单的任务.

有一个类似的问题,但它没有解决这个问题.为什么 - [[UIButton appearance] setBackgroundImage]影响UIBarItem对象的初始外观,你如何纠正它?

Dis*_*Dev 4

我之前使用过的一种解决方案是清除 UINavigationBar 中包含的 UIButtons 的“backgroundImage”属性:

[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setBackgroundImage:nil forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

这应该允许您在其他情况下自定义 UIButton,而无需触摸 UINavigationBar 中 UIBarButtonItem 内的按钮。