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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在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;

}

以上是关于在Attribute Inspector 上显示自定义的控件的属性的主要内容,如果未能解决你的问题,请参考以下文章