How to set default values for IBInspectable in Objective-C?

前端 未结 5 925
面向向阳花
面向向阳花 2021-01-30 16:15

I know default values of IBInspectable-properties can be set as:

@IBInspectable var propertyName:propertyType = defaultValue in Swift. But how do I achieve

5条回答
  •  太阳男子
    2021-01-30 16:28

    Firstly I tried to override getter and did something like this:

    - (UIColor *)borderColor {
        return _borderColor ?: [UIColor greenColor];
    }
    

    But in that case I received issue about undeclared identifier _borderColor.

    Ok, I tried to avoid this issue via custom getter.

    - (UIColor *)getBorderColor {
        return _borderColor ?: [UIColor greenColor];
    }
    

    Actually it's not a proper getter, as we don't point this method as getter. In case we point we'll receive issue about undeclared identifier, that's why we won't.

    Then we use this method for getting property value in updateUI method.

    Also we have to override setter:

    - (void)setBorderColor:(UIColor *)borderColor {
      _borderColor = borderColor;
      [self updateUI];
    }
    

提交回复
热议问题