@IBInspectable with enum?

前端 未结 7 1369
旧时难觅i
旧时难觅i 2020-12-24 04:11

I\'d like to create @IBInspectable element as you see at the picture below :

\"enter

相关标签:
7条回答
  • 2020-12-24 05:00

    I do this using a Inspectable NSInteger value and override the setter to allow it to set the enum. This has the limitation of not using a popup list and if you change your enum values, then the interface options will not update to match.

    Example.

    In Header File:

    typedef NS_ENUM(NSInteger, LabelStyle)
    {
        LabelStyleContent = 0, //Default to content label
        LabelStyleHeader,
    };
    
    ...
    
    @property LabelStyle labelStyle;
    @property (nonatomic, setter=setLabelAsInt:) IBInspectable NSInteger labelStyleLink;
    

    In the implementation file:

    - (void)setLabelAsInt:(NSInteger)value
    {
        self.labelStyle = (LabelStyle)value;
    }
    

    You could optionally add some logic in there to ensure that it is being set to a valid value

    0 讨论(0)
提交回复
热议问题