#import <UIKit/UIKit.h>
#import "CustomUIViewModel.h"
@interface CustomUIView : UIView
// 步骤3 提供模型
@property (nonatomic, strong)CustomUIViewModel *model;
@end
.m文件
#import "CustomUIView.h"
@interface CustomUIView()
@property (nonatomic,weak) UILabel *lable;
@end
@implementation CustomUIView
// 步骤 1:重写initWithFrame:方法,创建子控件并 - 添加
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UILabel *lable = [[UILabel alloc] init];
self.lable = lable;
[self addSubview:lable];
}
return self;
}
// 步骤 2:重写layoutSubviews,子控件设置frame
- (void)layoutSubviews {
[super layoutSubviews];
CGSize size = self.frame.size;
self.lable.frame = CGRectMake(0, 0, size.width * 0.5, size.height * 0.5);
}
// 步骤 4: 子控件赋值
- (void)setModel:(CustomUIViewModel *)model {
_model = model;
self.lable.text = model.name;
}
@end