在Attribute Inspector 上显示自定义的控件的属性

FirstColor 跟 CornerRadious 都是新增的显示属性具体实现方法如下:

@property(nonatomic,weak)IBInspectable UIColor *firstColor;

/...................................../

- (void)setFirstColor:(UIColor *)firstColor{

    _firstColor = firstColor;

     self.backgroundColor = firstColor;

}

在定义的属性面前增加IBInspectable关键字 (视图已经拖到控制器上)

如果想让所有的View的某个属性都能在IB上显示的话可以新建View的category

@interface UIView (IBIspectable)

@property(nonatomic,assign)IBInspectable CGFloat cornerRadious;

/............在implementation添加IB_DESIGNABLE关键字........................./

IB_DESIGNABLE

@implementation UIView (IBIspectable)

- (void)setCornerRadious:(CGFloat)cornerRadious{

    self.layer.cornerRadius = cornerRadious;

    self.layer.masksToBounds = cornerRadious>0;

}

- (CGFloat)cornerRadious{

     return self.layer.cornerRadius;

}

原文地址:https://www.cnblogs.com/cnman/p/5224206.html