#一、安裝html
pod 'Masonry'
#二、引入頭部git
#import "Masonry.h"
#三、demogithub
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //添加兩個控件 UIView *blueView = [[UIView alloc] init]; blueView.backgroundColor = [UIColor blueColor]; blueView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:blueView]; UIView *redView = [[UIView alloc] init]; redView.backgroundColor = [UIColor redColor]; redView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:redView]; //給藍色View設置約束 [blueView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.mas_left).offset(30);//和父view的左邊間距爲30; make.bottom.equalTo(self.view.mas_bottom).offset(-30);//和父view的底部間距爲30; make.right.equalTo(redView.mas_left).offset(-30);//和紅色view的間距爲30; make.height.mas_equalTo(50);//藍色view的高度爲50 }]; //給紅色View設置約束 [redView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.view.mas_right).offset(-30);//和父view的右邊間距爲30; make.bottom.equalTo(blueView.mas_bottom);//和藍色view的底部對齊 make.height.equalTo(blueView.mas_height);//和藍色view的高度相等 make.width.equalTo(blueView.mas_width);//和藍色view的寬度相等 }]; }
#四、效果圖
官方git地址:https://github.com/SnapKit/Masonry
參考文章
一、http://www.jianshu.com/p/d172131d24c9
二、http://www.mamicode.com/info-detail-470300.htmlcode