ios - 使用 Interface Builder 子类化与类别

标签 ios interface-builder

我多次读到我们不应该将组件(例如 UIButton)子类化:

问题出在我使用 Interface Builder 时。 例如,我有一个按钮在我的很多 View 中都有精确的外观。我可以每次使用 IB 设置它们(这很痛苦),或者我可以使用自定义类来分解自定义行为和外观。

在我看来,简化 IB 流程的唯一方法就是按照每个人都反对的方式去做,这似乎有点矛盾。

有更好的解决方案吗?我可以在 IB 中使用类别吗?

谢谢。

最佳答案

您可以使用 UIView 外观代理。我不知道您对按钮做了什么,但这可能会有所帮助:

将这是您的 AppDelegate 文件放入 application:didFinishLaunchingWithOptions: 方法

if([UIButton conformsToProtocol:@protocol(UIAppearanceContainer)]){
    [[UIButton appearance] setBackgroundImage:[UIImage imageNamed:@"YourImage"] forState:UIControlStateNormal];
    //modify any other UIButton properties you want to set globally
}

关于ios - 使用 Interface Builder 子类化与类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17997559/

相关文章:

iphone - Interface Builder 的 'Class Actions' 面板中的 'Object Identity' 在哪里?

objective-c - 如何为 Interface Builder 插件指定绑定(bind)?

objective-c - 加载 .xib 时出错 - 应用程序崩溃

Xcode,自定义 View - 零帧/边界

ios - UITableViewCell 向右滑动触发 Action 或执行 segue

objective-c - 如何在 Objective-C 中为 IBInspectable 设置默认值?

ios - 设置UILabel的背景为透明黑色

ios - 根据 UILabel 文本的数量增加 tableview 单元格的高度

ios - 无法使用类型为 '[String : Any]' 的索引下标类型为 'UIImagePickerController.InfoKey' 的值

ios - 如何在swift中从被调用方法中识别调用方法