OC时使用 Mansory进行手写代码不加,swift时虽然也可以混编,但是有些麻烦,可以使用 SnapKit,
调用方法很类似。
以UILabel为例子。
OC下对UILabel布局为:
@property (nonatomic,strong) UILabel *userLbl;
[self.view addSubview:self.userLbl];
[self.userLbl mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view).offset(30);
make.top.mas_equalTo(self.view.mas_bottom).mas_offset(-120);
}];
- (UILabel *)userLbl{
if (!_userLbl) {
_userLbl = [[UILabel alloc]init];
_userLbl.text = "I am mansory for autolayout"
}
return _userLbl;
}
swift下:
self.view.addSubview(self.userLbl)
self.userLbl.snp.makeConstraints { make in
make.left.equalTo(self.view).offset(30)
make.top.equalTo(self.view.snp.bottom).offset(-120)
}
lazy var userLbl: UILabel = {
let label = UILabel()
label.text = "I am snapkit for autolayout"
return label
}()
是不是很简单