Masonry是一個輕量級的布局框架 擁有自己的描述語法 采用更優(yōu)雅的鏈?zhǔn)秸Z法封裝自動布局 簡潔明了 并具有高可讀性 而且同時支持 iOS 和 Max OS X。Masonry現(xiàn)在是使用很廣泛的第三方做約束和適配的庫,很強(qiáng)大。masonry使用起來還非常的方便雷激。
經(jīng)常犯的錯誤---注意事項:使用mosonry添加約束根穷,尤其是父子控件,他們之間的關(guān)系一定要在添加約束之前addSubviews:庶艾。否則約束添加不成功兽赁。
一.錯誤信息統(tǒng)計(width改為with)
1.reason: 'Attributes should be chained before defining the constraint relation'
崩潰到masonry內(nèi)部的方法里面:
崩潰的提示信息:
直接上代碼:(這是運行沒有問題的代碼)
[self.GradientLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.CurrenPriceLabel.mas_right);
make.left.equalTo(self.VariationLabel.mas_left).with.offset(30);//更改的是此處的width變?yōu)閣ith状答,否則會報錯
make.width.equalTo(@60);//此處的width不需要改動
make.height.mas_equalTo(@30);
}];
注意:解決方法將width更改為with即可冷守。并不是全部的width都要改變,注意看上面的代碼部分惊科。
二.錯誤信息統(tǒng)計(父子控件之間的關(guān)系沒有建立好)
2.1拍摇、reason:couldn't find a common superview for
解決方法:查---好自己做約束的父子控件之間的關(guān)系是否建立起來了。
UITextField *nameTextField = [UITextField new];
nameTextField.font = [UIFont systemFontOfSize:14];
nameTextField.placeholder = @"請再次輸入密碼";
//父子控件的建立好關(guān)系:self.testView為父控件馆截,nameTextField為子控件
[self.testViewaddSubview: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);
}];
來源于:http://www.reibang.com/p/b2d1093eaca9