【问题标题】:How to set appearance for a UIButton without affecting UIBarButtonItems?如何在不影响 UIBarButtonItem 的情况下设置 UIButton 的外观?
【发布时间】:2012-09-30 17:51:22
【问题描述】:

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

[[UIButton appearance] setBackgroundImage:greenButtonImage forState:UIControlStateNormal];

我确实希望自定义后面的元素,只希望自定义常规的圆形矩形按钮。我意识到使用 appearanceWhenContainedIn: 可用于为 UIBarButtonItems 和 UITextField 设置自定义外观,但我希望这些按钮保持标准。子类化在这里不是一个选项,因为这样一个简单的任务不需要它。

有一个类似的问题,但它没有解决问题。 Why does -[[UIButton appearance] setBackgroundImage] affect the initial appearance of UIBarItem objects and how do you correct it?

【问题讨论】:

  • 我能够通过专门将这些设置为清晰的背景图像来解决 UITextField 问题。虽然不是一个好的解决方案。
  • 你是如何初始化你的 UIBarButtonItems 的?您是否将“customView”属性设置为 UIButton 对象?如果您可以提供有关如何创建 UIBarButtonItem 的示例代码,那么我很确定我可以提供帮助......我在我的应用程序中使用外观代理和条形按钮/UIButtons 进行了大量自定义,并且没有遇到这些问题。跨度>

标签: objective-c ios ios5 appearance uiappearance


【解决方案1】:

我以前使用过的一个解决方案是取消 UINavigationBar 中包含的 UIButtons 的“backgroundImage”属性:

[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setBackgroundImage:nil forState:UIControlStateNormal];

这应该允许您在其他情况下自定义 UIButtons,而无需触及 UINavigationBar 中的 UIBarButtonItem 内的那些。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2023-04-02
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多