参考文章:
http://adad184.com/2014/09/28/use-masonry-to-quick-solve-autolayout/
Demo地址:https://github.com/LiuDongiOS/MasonryDemo
入门进阶表:(一定要先addSubview)
1:不堪一击,实现以下效果:
代码实现:
UIView *sv = [UIView new];
[sv showPlaceHolder];
sv.backgroundColor = [UIColor blackColor];
[self.view addSubview:sv];
[sv mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(@0);
make.height.equalTo(@200);
make.center.equalTo(self.view);
}];
2:初学乍练,实现�以下效果:
代码实现:
UIView *sv1 = [UIView new];
[sv1 showPlaceHolder];
sv1.backgroundColor = [UIColor redColor];
[sv addSubview:sv1];
[sv1 mas_makeConstraints:^(MASConstraintMaker *make) {
// 第一种方式
// make.top.equalTo(sv).with.offset(10);
// make.left.equalTo(sv).with.offset(10);
// make.right.equalTo(sv).with.offset(-10);
// make.bottom.equalTo(sv).with.offset(-10);
//第二种方法
//基于某个view四周进行约束
// make.edges.equalTo(sv).insets(UIEdgeInsetsMake(20, 10, 20, 10));
//
//第三种方法
make.top.and.left.and.bottom.and.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
}];
3:初窥门径,实现以下效果:
![让两个高度为150的view(sv的子view)水平居中且等宽且等间隔排列 间隔为10(自动计算其宽度).png . . .]](http://upload-images.jianshu.io/upload_images/1259755-79f1e17503f0375f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
代码实现:
int padding1 = 10;
UIView *sv2 = [UIView new];
sv2.backgroundColor = [UIColor colorWithRed:0.000 green:1.000 blue:0.502 alpha:1.000];
[sv2 showPlaceHolder];
[self.view addSubview:sv2];
UIView *sv3 = [UIView new];
sv3.backgroundColor = [UIColor colorWithRed:0.000 green:1.000 blue:0.502 alpha:1.000];
[sv3 showPlaceHolder];
[self.view addSubview:sv3];
[sv2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(150);
make.centerY.mas_equalTo(sv.mas_centerY);
make.width.equalTo(sv3);
make.left.equalTo(sv).with.offset(padding1);
make.right.equalTo(sv3.mas_left).with.offset(-padding1);
}];
[sv3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(@150);
make.centerY.mas_equalTo(sv.mas_centerY);
make.width.equalTo(sv2);
make.left.equalTo(sv2.mas_right).with.offset(padding1);
make.right.equalTo(sv.mas_right).with.offset(-padding1);
}];
4.略有小成,实现以下效果:
代码实现:
UIView *blackView = [UIView new];
blackView.backgroundColor = [UIColor blackColor];
[blackView showPlaceHolder];
[self.view addSubview:blackView];
UIView *grayView = [UIView new];
grayView.backgroundColor = [UIColor lightGrayColor];
[grayView showPlaceHolder];
[self.view addSubview:grayView];
[blackView mas_makeConstraints:^(MASConstraintMaker *make) {
//添加左,上边距的约束
//mas_equal的支持CGSIZE CGPOINT NSNUMBER UIEDGEINSETS
make.top.mas_equalTo(80);
make.left.mas_equalTo(20);
//添加右边约束
make.right.mas_equalTo(-20);
}];
[grayView mas_makeConstraints:^(MASConstraintMaker *make) {
//添加右下约束
make.bottom.right.mas_equalTo(-20);
//添加高度约束,让高度等于blackview
make.height.equalTo(blackView);
//添加上边约束
make.top.equalTo(blackView.mas_bottom).offset(20);
//添加左边距
make.left.equalTo(self.view.mas_centerX).offset(0)
;
}];
5:驾轻就熟,实现以下效果:
代码实现:
//第一步:
UIView *greenView = [UIView new];
greenView.backgroundColor = [UIColor greenColor];
[greenView showPlaceHolder];
[self.view addSubview:greenView];
UIView *redView = [UIView new];
redView.backgroundColor = [UIColor redColor];
[redView showPlaceHolder];
[self.view addSubview:redView];
UIView *blueView = [UIView new];
blueView.backgroundColor = [UIColor blueColor];
[blueView showPlaceHolder];
[self.view addSubview:blueView];
//第二步
[greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(80);
make.left.mas_equalTo(20);
make.right.mas_equalTo(self.view.mas_centerX).offset(-10);
}];
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(80);
make.right.mas_equalTo(-20);
make.left.mas_equalTo(greenView.mas_right).offset(20);
}];
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(redView.mas_bottom).offset(20);
make.left.mas_equalTo(20);
make.bottom.right.equalTo(@-20);
make.height.mas_equalTo(redView);
make.height.mas_equalTo(greenView);
}];
6.融会贯通,实现以下效果.
代码实现:
//这里我把距离bottom的距离设定了定值了在横屏键盘消失的时候会回到屏幕上方,
//这里让大家理解更新约束的用法.
- (void)viewDidLoad {
[super viewDidLoad];
[self creatBackButton];
self.textField = [UITextField new];
self.textField.backgroundColor = [UIColor redColor];
self.textField.frame = CGRectMake(10, self.view.frame.size.height /2, self.view.frame.size.width - 20, 40);
[self.textField showPlaceHolder];
[self.view addSubview:self.textField];
[self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-10);
make.centerX.equalTo(self.view);
make.bottom.mas_equalTo(-350);
make.height.mas_equalTo(40);
}];
// 注册键盘通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
// Do any additional setup after loading the view.
}
#pragma mark -- 键盘即将出现 --
- (void)keyboardWillChangeFrameNotification:(NSNotification *)notification {
// 获取键盘基本信息(动画时长与键盘高度)
NSDictionary *userInfo = [notification userInfo];
CGRect rect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(rect);
CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改下边距约束
[self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-keyboardHeight);
}];
// 更新约束
[UIView animateWithDuration:keyboardDuration animations:^{
[self.view layoutIfNeeded];
}];
}
#pragma mark -- 键盘即将消失 --
- (void)keyboardWillHideNotification:(NSNotification *)notification {
// 获得键盘动画时长
NSDictionary *userInfo = [notification userInfo];
CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改为以前的约束
[self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-350);
}];
// 更新约束
[UIView animateWithDuration:keyboardDuration animations:^{
[self.view layoutIfNeeded];
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.view endEditing:YES];
}
7.炉火纯青,实现以下效果.
代码实现:
UIView *sv = [UIView new];
sv.backgroundColor = [UIColor blackColor];
[self.view addSubview:sv];
[sv mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(@0);
make.top.equalTo(@80);
make.bottom.equalTo(@0);
}];
UIScrollView *scrollView = [UIScrollView new];
scrollView.backgroundColor = [UIColor whiteColor];
[sv addSubview:scrollView];
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(sv);
}];
UIView *container = [UIView new];
[scrollView addSubview:container];
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
make.width.equalTo(scrollView);
}];
int count = 50;
UIView *lastView = nil;
for ( int i = 1 ; i <= count ; ++i )
{
UIView *subv = [UIView new];
[container addSubview:subv];
subv.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 )
saturation:( arc4random() % 128 / 256.0 ) + 0.5
brightness:( arc4random() % 128 / 256.0 ) + 0.5
alpha:0.6];
[subv mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(container);
make.height.mas_equalTo(@(5*i + 5));
//作答区
if (lastView) {
make.top.mas_equalTo(lastView.mas_bottom);
}else{
make.top.mas_equalTo(container.mas_top);
}
}];
lastView = subv;
[lastView showPlaceHolder];
}
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom);
}];
8:出类拔萃,实现以下效果:
实现代码:
//主要代码
- (void)handleMake:(UIButton *)button{
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(80);
make.left.mas_equalTo(self.view.mas_left);
make.bottom.right.mas_equalTo(0);
}];
}
- (void)handleUp:(UIButton *)button{
//更新你需要更新的约束
[self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(150);
}];
}
- (void)handleRemark:(UIButton *)button{
//不能针对一个view添加俩个约束
[self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(180);
make.left.mas_equalTo(self.view.mas_left);
make.bottom.right.mas_equalTo(-80);
}];
}
9:出神入化,实现以下效果:
代码实现:
//用到了自定义cell 工程demo在下方链接
[self.lableContent mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.contentView);
}];
10:得心应手,实现以下效果:
代码实现:
//此代码可以直接放在viewDidLoad
// 创建一个空view 代表上一个view
__block UIView *lastView = nil;
// 间距为10
int intes = 10;
// 每行3个
int num = 3;
// 循环创建view
for (int i = 0; i < 9; i++) {
UIView *view = [UIView new];
[self.view addSubview:view];
view.backgroundColor = [UIColor colorWithHue:(arc4random() % 256 / 256.0 ) saturation:( arc4random() % 128 / 256.0 ) + 0.5
brightness:( arc4random() % 128 / 256.0 ) + 0.5 alpha:0.2];
// 添加约束
[view mas_makeConstraints:^(MASConstraintMaker *make) {
// 给个高度约束
make.height.mas_equalTo(80);
// 1. 判断是否存在上一个view
if (lastView) {
// 存在的话宽度与上一个宽度相同
make.width.equalTo(lastView);
} else {
if (i % num != 0) {
make.width.mas_equalTo((view.superview.frame.size.width - (num + 1)* intes)/4);
}
}
// 2. 判断是否是第一列
if (i % num == 0) {
// 一:是第一列时 添加左侧与父视图左侧约束
make.left.mas_equalTo(view.superview).offset(intes);
} else {
// 二: 不是第一列时 添加左侧与上个view左侧约束
make.left.mas_equalTo(lastView.mas_right).offset(intes);
}
// 3. 判断是否是最后一列 给最后一列添加与父视图右边约束
if (i % num == (num - 1)) {
make.right.mas_equalTo(view.superview).offset(-intes);
}
// 4. 判断是否为第一列
if (i / num == 0) {
// 第一列添加顶部约束
make.top.mas_equalTo(view.superview).offset(intes*10);
} else {
// 其余添加顶部约束 intes*10 是我留出的距顶部高度
make.top.mas_equalTo(intes * 10 + ( i / num )* (80 + intes));
}
}];
// 每次循环结束 此次的View为下次约束的基准
lastView = view;
}