自定义 Class 的 UIButton 并在具有枚举的 StoryBoard 中显示它

Custom Class of UIButton and show it in StoryBoard having enum also

我想创建 UI 按钮的子 class。我想在那里提供一些额外的功能。在此 class 中,我使用的枚举也类似于 UIButtonType UIButton class 中的 UIButtonType。我还想在故事板 UI 中显示此选项,开发人员可以从那里 select 自定义按钮的类型 class。请建议我,我如何才能做到这一点。

您需要将继承的 UIButton class 指定为 IBDesignable 并添加一个 NSInteger IBInspectable 属性 InterfaceBuilder 将对其进行分析以便显示。

但是,Interface Builder 无法呈现枚举类型 IBInspectable 属性;这就是为什么实现此目的的唯一方法是 NSInteger 属性。

最后,您将 select 在界面生成器中为您的自定义按钮(0、1 ... YourEnumMaxValue)提供一个与枚举值相对应的整数值。最后,你只需要实现一些代码来按照 IB 中选择的值呈现你的按钮。

关于 LiveRendering 的更多信息:Creating a Custom View That Renders in Interface Builder