之前我们在屏幕适配的章节中学习过 AutoLayout
的使用,但那都是在可视化界面上进行添加约束完成的,我们很多时候都需要在代码中使用 AutoLayout
约束,苹果也为我们提供了实现,使用 NSLayoutConstraint
类表示约束,但使用起来比较复杂,代码量比较大 ,Masonry
是一个轻量级的布局框架,它拥有自己的描述语法(采用更优雅的链式语法封装)来自动布局,具有很好可读性且同时支持 iOS和Max OS X
等。总之,对于侧重写代码的coder,请你慢慢忘记 Frame
,喜欢 Masonry
吧。下面我就登录框做一个小例子。使用前需要导入Masonry 框架 使用pods 直接拖进工程都行都行,
直接进入Github搜索Masonry进行源码下载使用CocoaPod进行下载
#import "Masonry.h"
UILabel *label = [[UILabel alloc] init];
label.text = @"用户名:";
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor greenColor];
label.font = [UIFont systemFontOfSize:15];
[self.view addSubview:label];
UILabel *label1 = [[UILabel alloc] init];
label1.text = @"密 码:";
label1.textAlignment = NSTextAlignmentCenter;
label1.textColor = [UIColor blackColor];
label1.backgroundColor = [UIColor greenColor];
label1.font = [UIFont systemFontOfSize:15];
[self.view addSubview:label1];
UITextField *tf = [[UITextField alloc] init];
tf.backgroundColor = [UIColor greenColor];
tf.placeholder = @"请输入用户名";
tf.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:tf];
UITextField *tf1 = [[UITextField alloc] init];
tf1.backgroundColor = [UIColor greenColor];
tf1.placeholder = @"请输入密码";
tf1.secureTextEntry = YES;
tf1.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:tf1];
UIButton *but =[UIButton buttonWithType:UIButtonTypeCustom];
[but setTitle:@"登录" forState:UIControlStateNormal];
[but setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[but setBackgroundColor:[UIColor redColor]];
[self.view addSubview:but];
UIButton *but1 =[UIButton buttonWithType:UIButtonTypeCustom];
[but1 setTitle:@"注册" forState:UIControlStateNormal];
[but1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[but1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:but1];
__weak typeof(self) vc = self;
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(vc.view).offset(100);
make.left.equalTo(vc.view).offset(50);
make.height.equalTo([NSNumber numberWithInt:30]);
make.width.equalTo([NSNumber numberWithInt:60]);
}];
[label1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(label.mas_bottom).offset(5);
make.left.equalTo(label);
make.height.equalTo(label);
make.width.equalTo(label);
}];
[tf mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.height.equalTo(label);
make.left.equalTo(label.mas_right).offset(10);
make.right.equalTo(vc.view).offset(-50);
}];
[tf1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.height.equalTo(label1);
make.left.equalTo(label1.mas_right).offset(10);
make.right.equalTo(vc.view).offset(-50);
}];
[but mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(tf1.mas_bottom).offset(10);
make.left.equalTo(label1);
make.height.equalTo(label1);
make.right.equalTo(tf1);
}];
[but1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(but.mas_bottom).offset(10);
make.left.equalTo(but);
make.height.equalTo(but);
make.right.equalTo(but);
}];