我多次读到我们不应该将组件(例如 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/