一、Masonry其實就是對系統的Autolayout 進行了封裝、大大的提升了開發效率 編程
二、Masonry用到的是鏈式編程思想spa
#import "ViewController.h" #import <Masonry.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIView *red = [[UIView alloc] init]; red.backgroundColor = [UIColor redColor]; [self.view addSubview:red]; //masonry 內部默認設置了 translatesAutoresizingMaskIntoConstraints red.translatesAutoresizingMaskIntoConstraints = NO; [red mas_makeConstraints:^(MASConstraintMaker *make) { // 如下幾種寫法都同樣 1。make.left.mas_equalTo(self.view.mas_left).multipliedBy(1.0).with.offset(20); // 2 make.left.mas_equalTo(self.view.mas_left).offset(20); // 3 make.left.mas_equalTo(self.view).offset(20); // 4 make.left.offset(20); // 5 make.left.and.top.offset(20); // make.left.top.offset(20); and能夠省略 // make.right.mas_equalTo(self.view).offset(-20); // make.top.mas_equalTo(self.view).offset(20); // make.bottom.mas_equalTo(self.view).offset(-20); make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(20, 20, 20, 20)); }]; //更新約束 [red mas_updateConstraints:^(MASConstraintMaker *make) { make.bottom.offset(-100); }]; //刪除以前的約束 從新設置約束 [red mas_remakeConstraints:^(MASConstraintMaker *make) { }]; // Do any additional setup after loading the view. } @end