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

前端 未结 5 918
面向向阳花
面向向阳花 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:25

    I wrote my code like this. It works pretty well for me, both when designing in the interface builder or running as a app.

    @interface MyView : UIView
    
    @property (copy, nonatomic) IBInspectable propertyType *propertyName;
    
    @end
    
    - (void)makeDefaultValues {
        _propertyName = defaultValue;
        //Other properties...
    }
    
    - (instancetype)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            [self makeDefaultValues];
        }
        return self;
    }
    
    - (instancetype)initWithCoder:(NSCoder *)aDecoder {
        if (self = [super initWithCoder:aDecoder]) {
            [self makeDefaultValues];
        }
        return self;
    }
    

提交回复
热议问题