目录
方法2:使用@IBInspectable(下面以UIButton为例介绍使用方法)
Step1:创建一个UIButton分类(Category)UIButton+Layer
Step3:UIButton+Layer.m中实现setter、getter方法
Step4:到这里前期准备就做完了,选中storyboard中你的任意一个UIButton,都能在下图中简单地设置
方法1:Runtime Attributes
设置圆角:layer.cornerRadius
设置边宽:layer.borderWidth
缺点:keyPath需要手敲,容易拼写错误,需要设置多个控件时相对比较麻烦。
方法2:使用@IBInspectable(下面以UIButton为例介绍使用方法)
Step1:创建一个UIButton分类(Category)UIButton+Layer
Step2:UIButton+Layer.h 中添加属性
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIButton (Layer)
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;
@property (nonatomic, assign) IBInspectable CGFloat borderWidth;
@end
NS_ASSUME_NONNULL_END
Step3:UIButton+Layer.m中实现setter、getter方法
#import "UIButton+Layer.h"
@implementation UIButton (Layer)
/************ cornerRadius ************/
- (void)setCornerRadius:(CGFloat)cornerRadius {
self.layer.cornerRadius = cornerRadius;
}
- (CGFloat)cornerRadius {
return self.layer.cornerRadius;
}
/************ borderWidth ************/
- (void)setBorderWidth:(CGFloat)borderWidth {
self.layer.borderWidth = borderWidth;
}
- (CGFloat)borderWidth {
return self.layer.borderWidth;
}
@end
Step4:到这里前期准备就做完了,选中storyboard中你的任意一个UIButton,都能在下图中简单地设置
step5:来看看效果
*************************************************************************************************
总结:
1、方法1的Runtime Attributes在针对个别控件设置地时候还是比较适用的,需要注意拼写别出错;
2、方法2的@IBInspectable在项目中有多个控件需要设置一些属性时,结合category就显得非常方便了,一劳永逸;
3、根据@IBInspectable的特点,还可以做其它方面的延伸~
日常开发使用心得整理,如有纰漏,欢迎指正!!!