級別: ★☆☆☆☆
標簽:「iOS Masonry」「iOS 自動布局」「Masonry」
作者: Xs·H
審校: QiShare團隊
在 沐靈洛 線下分享iOS UIButton根據內容自動布局時,有和前端同學討論到iOS的常用布局方式。討論過程十分熱鬧,不容易記錄,但作者認為討論結果有必要記錄一下,希望能幫助到一些同學。
作者將iOS常用布局方式歸納為Frame、Autoresizing、Constraint、StackView和Masonry五種,并將逐一介紹。
本篇文章介紹Masonry。
在iOS 常用布局方式之Constraint文章中,作者介紹了NSLayoutConstraint在布局界面時的強大功能,但也體驗到了NSLayoutConstraint代碼語法的冗長和不易閱讀性。本文要介紹的Masonry就是一個輕量級的布局框架,它使用更好的語法包裝AutoLayout,并提供了一種描述NSLayoutConstraint的可鏈接方式,從而使布局代碼更簡潔,更易讀。
所以說,Masonry是基于NSLayoutConstraint的一種第三方框架,可以從GitHub上查看其詳細介紹。這里,作者只展示一下使用Masonry實現4分圖效果的代碼。
@implementation QiMasonryViewController
- (void)viewDidLoad {
[super viewDidLoad];
_contentView = [[QiMasonryContentView alloc] initWithFrame:CGRectZero];
_contentView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_contentView];
[_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top);
make.left.equalTo(self.view.mas_left);
make.right.equalTo(self.view.mas_right);
make.bottom.equalTo(self.view.mas_bottom).with.offset(-20.0);
}];
}
@end
@implementation QiMasonryContentView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_subView1 = [[UIView alloc] initWithFrame:CGRectZero];
_subView1.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:.6];
[self addSubview:_subView1];
_subView2 = [[UIView alloc] initWithFrame:CGRectZero];
_subView2.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:.6];
[self addSubview:_subView2];
_subView3 = [[UIView alloc] initWithFrame:CGRectZero];
_subView3.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:.6];
[self addSubview:_subView3];
_subView4 = [[UIView alloc] initWithFrame:CGRectZero];
_subView4.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:.6];
[self addSubview:_subView4];
[_subView1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(20);
make.left.mas_equalTo(10);
make.width.equalTo(self.subView2);
make.height.equalTo(self.subView3);
make.right.equalTo(self.subView2.mas_left).offset(-20);
}];
[_subView2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.subView1.mas_top);
make.right.equalTo(self).offset(-10);
make.width.equalTo(self.subView1);
make.height.equalTo(self.subView4);
}];
[_subView3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.subView1.mas_bottom).offset(20);
make.left.mas_equalTo(10);
make.width.mas_equalTo(self.subView4);
make.height.mas_equalTo(self.subView1);
make.bottom.mas_equalTo(self).offset(-20);
}];
[_subView4 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.subView3);
make.left.mas_equalTo(self.subView3.mas_right).offset(20);
make.right.mas_equalTo(self).offset(-10);
make.width.mas_equalTo(self.subView3);
make.height.mas_equalTo(self.subView2);
make.bottom.mas_equalTo(self).offset(-20);
}];
}
return self;
}
@end
上述代碼所實現的效果如下圖所示。
文章中的代碼可以從QiLayoutDemo中獲取。
iOS 常用布局方式總結
截止本篇文章,作者逐一介紹了Frame、Autoresizing、Constraint、StackView和Masonry五種iOS常用布局方式。作者的目的是通過對這幾種布局方式的介紹,能讓一些讀者同學對iOS布局有個全面的了解。至于常被問到的哪種布局方式好,實在是不好回答,但可以分享一下這些布局方式在項目中的使用情況,以及遇到的問題,供各位讀者同學參考。
-
1. Frame+Autoresizing
得益于Frame的編程靈活性和Autoresizing的簡易,Frame+Autoresizing的組合布局方式在QiShare項目中占比最高。但在一些只需要操作frame某個屬性(比如,只想修改寬度)時,會顯得代碼有點冗余。于是,引入UIView+QiAddition可以在一定程度上得以改善。如下。
_subView.qi_top = 10.0;
_subView.qi_left = 20.0;
_subView.qi_width = 300.0;
_subView.qi_height = 40.0;
2. 基于Storyboard的Constraint
Storyboard是個很方便的工具,在搭建一些簡單界面時,比Frame編碼要高效很多。結合AutoLayout的Constraint,可以快速搭建出帶有布局約束的界面。但是,在對AutoLayout概念和Constraint掌握不特別清晰時,常常會出現約束沖突的情況,而且排查困難。另外,打開Storyboard文件會需要更多的電腦性能,在作者的iMac上會常常出現卡頓的現象。3. 基于純代碼的Constraint+Masonry
三方庫Masonry是基于NSLayoutConstraint的布局方式,用純代碼的方式可以更易閱讀地實現控件約束。但由于NSLayoutConstraint與Frame的不兼容(使用NSLayoutConstraint約束過的控件,再修改frame會無效),在多人合作的項目中很少被用到。4. Frame+StackView 或 Constraint+StackView
StackView的使用,主要看有沒有界面需求適合排列布局方式,它不受限于Frame或者Constraint。但由于UIStackView在iOS9+才會有,所以最低支持iOS9-的項目中基本不會使用。隨著目前項目最低支持版本逐漸升到了iOS10+,UIStackView會在未來的項目中被越來越多地使用到。