IB_DESIGNABLE - 定義xib鏈接
IBInspectable - 鏈接屬性
以下都是個人理解
IB_DESIGNABLE
- 1 定義 這個類鏈接xib
IB_DESIGNABLE
@interface XIBView : UIView
@property (nonatomic, strong) UITextField *inputField;
@end
- 2 NS_DESIGNATED_INITIALIZER
注意這個屬性决记,需要使用這個標記的方法初始化類作谭,再能實現IB_DESIGNABLE哦袖扛。(坑了我還一會)
// 在類里面可以各種其他設置(順便有發(fā)現了人家的另外一種初始化方法)
-(instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_inputField = ({
UITextField *label = [[UITextField alloc]init];
label.text = @"2011";
label.frame = CGRectMake(0, 0, 100, 33);
label.backgroundColor = [UIColor yellowColor];
[self addSubview:label];
label;
});// 使用自動布局也可以的
}
return self;
}
// 趕緊到 xib喳篇,或者storyboard中繼承上面成類看看螃壤。
// 效果拔群片林。
// 代碼寫什么集漾,就顯示什么合呐,不要運行就能看(不過需要一小會時間相當于運行吧。唉)
IBInspectable
- 1 定義屬性到xib中 實時修改(例如在xib 中可以直接修改顏色等屬性)
@property (nonatomic, copy) IBInspectable UIColor *testColor;
- 2 set 方法
// 注意寫set方法漩勤。
- (void)setTestColor:(UIColor *)testColor {
_testColor = testColor;
self.backgroundColor = testColor;
}
- 3 實現
xib 中是不是多了個屬性号涯,還不趕緊試試
1