自 iPhone 6 之後,iOS 布局基本都走 Auto Layout。它固然好用,但有時候我們需要手動寫頁面布局,VFL(Visual Format Language)算是一種選擇。VFL 並不複雜,理解起來也容易,可一旦第一次接觸,往往要花點功夫才能寫順手。Masonry 可以看作 VFL 的簡化版,用的人很多,對習慣手寫頁面的開發者來說無疑是個福音。
先看一個常見問題:把一個子 View 按邊距擺到父視圖的某個位置。

如果用原生 VFL,代碼會是這樣(冗長且難讀):
UIView *superview = self.view;
UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor redColor];
[superview addSubview:view1];
UIEdgeInsets padding = UIEdgeInsetsMake(200, 50, 200, 50);
[superview addConstraints:@[
[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:padding.top],
[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:padding.left],
[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-padding.bottom],
[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:-padding.right],
]];
只是設個邊距就這麼囉嗦,一旦視圖關係複雜,維護起來簡直是噩夢。再看看 Masonry 怎麼寫——導入 Masonry.h 後:
UIView *childView = [UIView new];
[childView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:childView];
__weak typeof(self) weakSelf = self;
UIEdgeInsets padding = UIEdgeInsetsMake(200, 50, 200, 50);
[childView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(weakSelf.view).with.insets(padding);
}];
對比之下簡直是鳥槍換炮。下面就來看幾個更實用的功能。
1. 設置大小並居中:
UIView *childView = [UIView new];
[childView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:childView];
__weak typeof(self) weakSelf = self;
[childView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(100, 100)); // 設置大小
make.center.equalTo(weakSelf.view); // 居中
}];

這裡補充一個要點:mas_makeConstraints 用來創建約束,mas_updateConstraints 用來更新約束,mas_remakeConstraints 則會清除舊約束再添加新約束。官方英文說明如下:
// 創建約束,block 執行完後添加到 view 或合適的 superview - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; // 創建約束,若已存在相同約束則更新 - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block; // 創建約束,並移除該 view 之前的所有約束 - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
2. 設置高度(左右邊距,因此不設寬度):
UIView *childView = [UIView new];
[childView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:childView];
__weak typeof(self) weakSelf = self;
[childView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.view.mas_top).with.offset(44); // 距頂部 44
make.left.equalTo(weakSelf.view.mas_left).with.offset(30); // 距左邊 30
make.right.equalTo(weakSelf.view.mas_right).with.offset(-30);// 距右邊 30,注意是負數
make.height.mas_equalTo(@150); // 高度 150
}];

3. 子視圖之間的相對位置:
UIView *childView = [UIView new];
[childView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:childView];
__weak typeof(self) weakSelf = self;
[childView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.view.mas_top).with.offset(44);
make.left.equalTo(weakSelf.view.mas_left).with.offset(30);
make.right.equalTo(weakSelf.view.mas_right).with.offset(-30);
make.height.mas_equalTo(@150);
}];
UIView *nextView = [UIView new];
[nextView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:nextView];
[nextView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(childView.mas_bottom).with.offset(30); // 緊貼上一個 view 底部
make.right.equalTo(childView.mas_right).with.offset(-30);
make.width.mas_equalTo(@100);
make.height.mas_equalTo(@100);
}];

4. 鏈式寫法,更加簡潔:
UIView *childView = [UIView new];
[childView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:childView];
__weak typeof(self) weakSelf = self;
[childView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.left.mas_equalTo(weakSelf.view).with.offset(100);
make.bottom.and.right.mas_equalTo(weakSelf.view).with.offset(-100);
// 更簡單的第二種寫法,直接相對父視圖
// make.top.and.left.mas_equalTo(100);
// make.bottom.and.right.mas_equalTo(-100);
}];
UILabel *label = [UILabel new];
[label setText:@"博客園-FlyElephant"];
[label setTextColor:[UIColor redColor]];
[label setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(weakSelf.view).with.offset(10);
make.height.mas_equalTo(20);
make.right.mas_equalTo(weakSelf.view).with.offset(-10);
make.bottom.mas_equalTo(weakSelf.view).with.offset(-50);
}];

網上關於 Masonry 的教程很多,這幾種情況基本能覆蓋日常開發需求。Masonry 的屬性與 iOS 原生屬性是一一對應的,理解了對照關係便觸類旁通:
