github上的示例demo 很詳細
Masonry 源碼:https://github.com/Masonry/Masonry
介紹:
? ? ? Masonry是一個輕量級的布局框架 擁有自己的描述語法 采用更優雅的鏈式語法封裝自動布局 簡潔明了 并具有高可讀性 而且同時支持 iOS 和 Max OS X。Masonry現在是使用很廣泛的第三方做約束和適配的庫,很強大。masonry使用起來還非常的方便。
經常犯的錯誤---注意事項:使用mosonry添加約束,尤其是父子控件,他們之間的關系一定要在添加約束之前 addSubviews:。否則約束添加不成功。
一.錯誤信息統計(width改為with)
? 1.reason: 'Attributes should be chained before defining the constraint relation'
? ? ? 崩潰到masonry內部的方法里面:
? ? 崩潰的提示信息:
? ? ?直接上代碼:(這是運行沒有問題的代碼)
? ? ?[self.GradientLabel mas_makeConstraints:^(MASConstraintMaker *make) {
? ? ? ? ? ?make.right.equalTo(self.CurrenPriceLabel.mas_right);
? ? ? ? ? make.left.equalTo(self.VariationLabel.mas_left).with.offset(30);//更改的是此處的width變為with,否則會報錯
? ? ? ? ? make.width.equalTo(@60);//此處的width不需要改動
? ? ? ? ? make.height.mas_equalTo(@30);
? ? ?}];
注意:解決方法將width更改為with即可。并不是全部的width都要改變,注意看上面的代碼部分。
二.錯誤信息統計(父子控件之間的關系沒有建立好)
2.1、reason:couldn't find a common superview for<UIView: ...frame: ...layer: ...>
解決方法:查---好自己做約束的父子控件之間的關系是否建立起來了。
? ? ? ? ?UITextField *nameTextField = [UITextField new];
? ? ? ? ?nameTextField.font = [UIFont systemFontOfSize:14];
? ? ? ? ?nameTextField.placeholder = @"請再次輸入密碼";
? ? ? ? //父子控件的建立好關系:self.testView為父控件,nameTextField為子控件
? ? ? ? ?[self.testView addSubview:nameTextField];
? ? ? ? ?//開始約束
? ? ? ? [lable mas_makeConstraints:^(MASConstraintMaker *make) {
? ? ? ? make.left.mas_equalTo(self.testView.mas_left).with.offset(20);
? ? ? ? make.top.mas_equalTo(self.testView.mas_top).with.mas_offset(0);
? ? ? ? make.height.mas_equalTo(30);
? ? ? ? make.width.mas_equalTo(50);
? ? ?}];