iOS 原生的 NSLayoutConstraint 寫起來冗長,既拖慢開發節奏,可讀性也不理想,後續維護更是痛苦。工欲善其事必先利其器,一個順手的自動布局框架能讓效率成倍提升,ZXPAutoLayout 就是為了解決這個問題而出現的。
它採用鏈式語法,易擴展、可讀性強、維護成本低。所謂 auto layout,通俗說就是用零數學公式的方式適配各代 iPhone 機型並兼容橫豎屏。
用原生方式添加一個約束,需要這樣寫:
NSLayoutConstraint *constraint =
[NSLayoutConstraint constraintWithItem:view // 第一個 view
attribute:NSLayoutAttribute // 約束屬性:上下左右寬高間距等
relatedBy:NSLayoutRelationEqual // 相等/大於等於/小於等於
toItem:secondView // 參照的第二個 view
attribute:NSLayoutAttribute // 參照屬性
multiplier:multiplier // 比例 0~1
constant:0]; // 約束值隨便加一個約束就這麼長,而一個 view 起碼要有上邊距、左邊距和寬高,也就是 x、y、width、height 四個基本量——意味著上面這段代碼至少得重複四次。
同樣的需求,給一個 view 添加上邊距、左邊距與寬高:
// 半透明紅色背景 view,上下左右距離 superview 均為 10
UIView *bgView = [UIView new];
[self.view addSubview:bgView];
bgView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:.5];
[bgView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
layout.edgeInsets(UIEdgeInsetsMake(10, 10, 10, 10));
// 等價寫法,但要同時寫 top/left/bottom/right,推薦用上面這行更簡潔
// layout.topSpace(10).leftSpace(10).bottomSpace(10).rightSpace(10);
}];第一種,直接到 GitHub 下載:https://github.com/biggercoffee/ZXPAutoLayout
第二種,用 CocoaPods 搜索 ZXPAutoLayout,執行 pod search zxpautolayout 後安裝。注意有些本地索引的版本並非最新甚至搜不到,升級一下 CocoaPods 即可。
在需要的地方導入 ZXPAutoLayout.h 即可。下面幾組例子覆蓋了日常絕大部分場景。
UIView *bgView = [UIView new];
[self.view addSubview:bgView];
bgView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:.5];
[bgView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
layout.edgeEqualTo(self.view); // 位置與寬度等同 self.view
// 等價:layout.edgeInsets(UIEdgeInsetsMake(0, 0, 0, 0));
}];UIView *blueView = [UIView new];
[bgView addSubview:blueView];
blueView.backgroundColor = [UIColor blueColor];
[blueView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
layout.topSpace(20); // 距 superview 上邊距
layout.leftSpace(20); // 距 superview 左邊距
layout.rightSpace(20); // 距 superview 右邊距
layout.heightValue(100); // 高度
// 註意:設置了左右邊距會自動拉伸寬度,因此無需再設寬度
// 代碼可以寫成一行,但不推薦——可讀性差,一長串屬性也不利於 DEBUG
}];UIView *grayView = [UIView new];
[bgView addSubview:grayView];
grayView.backgroundColor = [UIColor grayColor];
[grayView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
// 上邊距參照 blueView 並加 10;第二個參數為 0 則緊貼其下邊
layout.topSpaceByView(blueView, 10);
// 左邊距等同 blueView;不為 0 時是在其基礎上再加偏移
layout.leftSpaceEqualTo(blueView, 0);
// 寬度等同 blueView,multiplier 為可選倍數,0.5 即取其一半
layout.widthEqualTo(blueView, 0).multiplier(0.5);
layout.heightValue(40);
}];只需要開 autoHeight():
UILabel *label = [UILabel new];
[self.view addSubview:label];
label.backgroundColor = [UIColor purpleColor];
label.textColor = [UIColor whiteColor];
label.text = @"這是文字自適應,這是文字自適應,這是文字自適應";
[label zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
layout.leftSpaceEqualTo(grayView, 10);
layout.bottomSpace(20);
layout.widthValue(100);
layout.autoHeight(); // 自適應高度,僅對 UILabel 有效
}];逐個設置,每個 view 的寬度都去等於相鄰 view,最後一個負責收右邊:
UIView *view1 = [UIView new];
[self.view addSubview:view1];
view1.backgroundColor = [UIColor blueColor];
UIView *view2 = [UIView new];
[self.view addSubview:view2];
view2.backgroundColor = [UIColor blackColor];
UIView *view3 = [UIView new];
[self.view addSubview:view3];
view3.backgroundColor = [UIColor blueColor];
[view1 zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
layout.topSpaceByView(grayView, 10);
layout.leftSpace(20);
layout.heightValue(40);
layout.widthEqualTo(view2);
}];
[view2 zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
layout.topSpaceEqualTo(view1, 0);
layout.leftSpaceByView(view1, 20);
layout.heightValue(40);
layout.widthEqualTo(view3);
}];
[view3 zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
layout.topSpaceEqualTo(view1, 0);
layout.leftSpaceByView(view2, 20);
layout.rightSpace(20);
layout.heightValue(40);
}];用 stack 容器只需要設置容器自身的位置與尺寸,內部子視圖的約束會自動補上:
ZXPStackView *stackView = [ZXPStackView new];
[self.view addSubview:stackView];
stackView.backgroundColor = [UIColor blackColor];
[stackView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
layout.topSpaceByView(grayView, 20);
layout.leftSpace(0);
layout.rightSpace(0);
layout.heightValue(100);
}];
UIView *view1 = [UIView new];
[stackView addSubview:view1];
view1.backgroundColor = [UIColor blueColor];
UIView *view2 = [UIView new];
[stackView addSubview:view2];
view2.backgroundColor = [UIColor yellowColor];
UIView *view3 = [UIView new];
[stackView addSubview:view3];
view3.backgroundColor = [UIColor redColor];
stackView.padding = UIEdgeInsetsMake(10, 10, 10, 10); // 內邊距
stackView.space = 10; // 子視圖間距
[stackView layoutWithType:ZXPStackViewTypeHorizontal];只需把布局類型換成 ZXPStackViewTypeVertical,其餘代碼完全一致,內部會依類型自動添加約束:
ZXPStackView *stackView = [ZXPStackView new];
[self.view addSubview:stackView];
stackView.backgroundColor = [UIColor blackColor];
[stackView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
layout.topSpaceByView(grayView, 20);
layout.leftSpace(0);
layout.rightSpace(0);
layout.heightValue(100);
}];
UIView *view1 = [UIView new];
[stackView addSubview:view1];
view1.backgroundColor = [UIColor blueColor];
UIView *view2 = [UIView new];
[stackView addSubview:view2];
view2.backgroundColor = [UIColor yellowColor];
UIView *view3 = [UIView new];
[stackView addSubview:view3];
view3.backgroundColor = [UIColor redColor];
stackView.padding = UIEdgeInsetsMake(10, 10, 10, 10);
stackView.space = 10;
[stackView layoutWithType:ZXPStackViewTypeVertical];要記住的一點:ZXPStackView 的子視圖不需要自己加約束,調用 layoutWithType: 時內部會統一處理。
本文梳理了 ZXPAutoLayout 的常用 API:如何設置單個 view 的約束、等寬等高、相對於某個 view 某一邊的定位,以及按固定值或比例計算寬高。有問題或寫得不到位的地方,歡迎留言交流。