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

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

    Since IBInspectable values are set after initWithCoder: and before awakeFromNib:, you can set the defaults in initWithCoder: method.

    @interface MyView : UIView
    @property (copy, nonatomic) IBInspectable NSString *myProp;
    @property (assign, nonatomic) BOOL createdFromIB;
    @end
    
    @implementation MyView
    
    - (id)initWithCoder:(NSCoder *)aDecoder {
        self = [super initWithCoder:aDecoder];
        if(self != nil) {
            self.myProp = @"foo";
            self.createdFromIB = YES;
        }
        return self;
    }
    
    - (void)awakeFromNib {
        if (self.createdFromIB) {
            //add anything required in IB-define way
        }
        NSLog(@"%@", self.myProp);
    }
    
    @end
    

提交回复
热议问题