大家都知道有些控件的属性是无法在Interface Builder中的属性面板中查看到的,只有通过代码来设置。比如UIView 的 cornerRadius,borderColor,borderWidth 等等。有没有办法让这些属性,或是我自定义的一些属性直接在Interface Builder中设置不写代码呢。下面就通过一个小demo来展示一下。
以UIView为例。 我新建一个CustomView继承UIView,里面我定义了三个属性,分别是cornerRadius,borderColor,borderWidth。

里面用到了两个关键字,IBInspectable,IBDesignable。简单点说 IBInspectable是用来标识你要定义的属性,IBDesignable是渲染你定义的属性可以在可视化面板中出现的。定义完这个CustomView后,接下来我们拉个uiview控件,把它的class改为CustomView。


这样定义的属性就可以在属性面板中直接设置了。还可以括展一些其它属性。这里就不举例了。
网友评论