IB_DESIGNABLE 和 IBInspectable 的用法
2016-12-02 17:28
344 查看
我们经常会在用一些自定义 UIView 来完成一些特殊的UI效果,但是怎么让我自定义的 UIView 在 Storyboard 中预览和修改一些自定义参数呢。这就需要用到两个吊吊的东西。
我们想一下
有了这些属性我们开始绘制。
OK,一个简单的圆形 UIView 就搞定了,现在我们想在 IB 中使用这个 CircleView,打开一个 Storyboard,拖一个 UIView 上去,然后设置 UIView 的 Class 为我们自定义的 CircleView。
![](//upload-images.jianshu.io/upload_images/1138002-550a363f510c2903.png?imageMogr2/auto-orient/strip%7CimageView2/2)
拖一个 UIView
![](//upload-images.jianshu.io/upload_images/1138002-c15905ed19244ae3.png?imageMogr2/auto-orient/strip%7CimageView2/2)
设置 Class
这时候我们是看不到预览效果的。
![](//upload-images.jianshu.io/upload_images/1138002-e3faf639a298a490.png?imageMogr2/auto-orient/strip%7CimageView2/2)
看不到预览
之后再回到 Storyboard 中,会重新 building 一下,就可以看到预览效果啦。
![](//upload-images.jianshu.io/upload_images/1138002-779dcf642a93b549.png?imageMogr2/auto-orient/strip%7CimageView2/2)
看到预览效果
当然需要设置 CircleView 的几个属性,上面代码中我并有写默认值,所以是什么都看不到的,默认值自己设置一下就好我就不贴代码。能预览了之后,下一步就是怎么在 Storyboard 中直接修改 CircleView 的属性,半径、颜色之类的。
OK,再回到 Storyboard 中,building 之后,选中我们的 UIView 元素,在右边切换到 Attributes inspector 栏目,就可以在顶部看到我们自定义的属性。
![](//upload-images.jianshu.io/upload_images/1138002-47e3bbf14ee3d531.png?imageMogr2/auto-orient/strip%7CimageView2/2)
自定义属性
Xcode 会根据不同的数据类型提供不同的输入框,我们可以随意修改属性,就可以看到预览效果。
![](//upload-images.jianshu.io/upload_images/1138002-0987a52b35d1a630.png?imageMogr2/auto-orient/strip%7CimageView2/2)
修改颜色
![](//upload-images.jianshu.io/upload_images/1138002-46a99186d9070e60.png?imageMogr2/auto-orient/strip%7CimageView2/2)
预览
![](//upload-images.jianshu.io/upload_images/1138002-f5767cb8fe15bef0.png?imageMogr2/auto-orient/strip%7CimageView2/2)
修改为填充
![](//upload-images.jianshu.io/upload_images/1138002-4107f22cad5bd2d8.png?imageMogr2/auto-orient/strip%7CimageView2/2)
实心圆形
文/鲍永章(简书作者)
原文链接:http://www.jianshu.com/p/64931d040692
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
IB_DESIGNABLE让你的自定 UIView 可以在 IB 中预览。
IBInspectable让你的自定义 UIView 的属性出现在 IB 中 Attributes inspector 。
自定义一个 UIView
我们先来自定义一个简单的 UIView,叫做CircleView,顾名思义,可以显示一个圆形的 UIView。
我们想一下
CircleView会有那些属性。
@interface CircleView : UIView @property (nonatomic, assign) CGFloat lineWidth; // 圆形线条的宽度 @property (nonatomic, assign) CGFloat radius; // 圆形的半径 @property (nonatomic, strong) UIColor *color; // 绘制的颜色 @property (nonatomic, assign) BOOL fill; // 是否填充,是不是实心圆 @end
有了这些属性我们开始绘制。
- (void)drawRect:(CGRect)rect { // 计算中心点 CGFloat centerX = (self.bounds.size.width - self.bounds.origin.x) / 2; CGFloat centerY = (self.bounds.size.height - self.bounds.origin.y) / 2; UIBezierPath *path = [[UIBezierPath alloc] init]; // 添加一个圆形 [path addArcWithCenter:CGPointMake(centerX, centerY) radius:_radius startAngle:0 endAngle:360 clockwise:YES]; // 设置线条宽度 path.lineWidth = _lineWidth; // 设置线条颜色 [_color setStroke]; // 绘制线条 [path stroke]; if (_fill) { // 如果是实心圆,设置填充颜色 [_color setFill]; // 填充圆形 [path fill]; } }
OK,一个简单的圆形 UIView 就搞定了,现在我们想在 IB 中使用这个 CircleView,打开一个 Storyboard,拖一个 UIView 上去,然后设置 UIView 的 Class 为我们自定义的 CircleView。
![](http://upload-images.jianshu.io/upload_images/1138002-550a363f510c2903.png?imageMogr2/auto-orient/strip%7CimageView2/2)
拖一个 UIView
![](http://upload-images.jianshu.io/upload_images/1138002-c15905ed19244ae3.png?imageMogr2/auto-orient/strip%7CimageView2/2)
设置 Class
这时候我们是看不到预览效果的。
![](http://upload-images.jianshu.io/upload_images/1138002-e3faf639a298a490.png?imageMogr2/auto-orient/strip%7CimageView2/2)
看不到预览
IB_DESIGNABLE
想要在 IB 中预览,只需要在自定义的 UIView 加上IB_DESIGNABLE修饰即可。
IB_DESIGNABLE @interface CircleView : UIView ... @end
之后再回到 Storyboard 中,会重新 building 一下,就可以看到预览效果啦。
![](http://upload-images.jianshu.io/upload_images/1138002-779dcf642a93b549.png?imageMogr2/auto-orient/strip%7CimageView2/2)
看到预览效果
当然需要设置 CircleView 的几个属性,上面代码中我并有写默认值,所以是什么都看不到的,默认值自己设置一下就好我就不贴代码。能预览了之后,下一步就是怎么在 Storyboard 中直接修改 CircleView 的属性,半径、颜色之类的。
IBInspectable
还记得上面我们定义的 CircleView 的几个属性吧,只要我们能在 Storyboard 中修改这几个属性值,那么 CircleView 就会绘制出我们想要的效果。我们只需要属性声明的时候加上IBInspectable修饰,Xcode 会自动添加到 Storyboard 中 Attributes inspector 栏目中。
@property (nonatomic, assign) IBInspectable CGFloat lineWidth; @property (nonatomic, assign) IBInspectable CGFloat radius; @property (nonatomic, strong) IBInspectable UIColor *color; @property (nonatomic, assign) IBInspectable BOOL fill;
OK,再回到 Storyboard 中,building 之后,选中我们的 UIView 元素,在右边切换到 Attributes inspector 栏目,就可以在顶部看到我们自定义的属性。
![](http://upload-images.jianshu.io/upload_images/1138002-47e3bbf14ee3d531.png?imageMogr2/auto-orient/strip%7CimageView2/2)
自定义属性
Xcode 会根据不同的数据类型提供不同的输入框,我们可以随意修改属性,就可以看到预览效果。
![](http://upload-images.jianshu.io/upload_images/1138002-0987a52b35d1a630.png?imageMogr2/auto-orient/strip%7CimageView2/2)
修改颜色
![](http://upload-images.jianshu.io/upload_images/1138002-46a99186d9070e60.png?imageMogr2/auto-orient/strip%7CimageView2/2)
预览
![](http://upload-images.jianshu.io/upload_images/1138002-f5767cb8fe15bef0.png?imageMogr2/auto-orient/strip%7CimageView2/2)
修改为填充
![](http://upload-images.jianshu.io/upload_images/1138002-4107f22cad5bd2d8.png?imageMogr2/auto-orient/strip%7CimageView2/2)
实心圆形
文/鲍永章(简书作者)
原文链接:http://www.jianshu.com/p/64931d040692
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
相关文章推荐
- IB_DESIGNABLE 和 IBInspectable 的用法
- IB_DESIGNABLE 与IBInspectable 补充
- 使用IB_DESIGNABLE与IBInspectable特性实现可在视图编辑器的功能栏中设置
- IB_DESIGNABLE和IBInspectable
- iOS学习笔记:神奇的IB_DESIGNABLE和IBInspectable
- iOS 神奇的IB_DESIGNABLE和IBInspectable
- iOS IBInspectable和IB_DESIGNABLE
- iOS 内存管理-copy、 retain、 assign 、readonly 、 readwrite、nonatomic、@property、@synthesize、@dynamic、IB_DESIGNABLE 、 IBInspectable、IBOutletCollection
- IB_DESIGNABLE 和 IBInspectable
- ### IB_DESIGNABLE 和 IBInspectable
- 神奇的IB_DESIGNABLE和IBInspectable
- IB_DESIGNABLE的使用
- IBInspectable / IBDesignable
- IBOutLet/IBAction/IBDesignable/IBInspectable
- @IBDesignable和@IBInspectable
- Xcode8.1中@IBDesignable以及@IBInspectable不能自动刷新的解决办法
- Swift中@IBDesignable/@IBInspectable的使用
- Xcode8.1中@IBDesignable以及@IBInspectable不能自动刷新的解决办法
- IBInspectable / IBDesignable
- iOS IBInspectable / IBDesignable的使用 可视化编程