[blueView mas_remakeConstraints:^(MASConstraintMaker *make) { }];這個辦法將會掩蓋以前的某些特定的約束
[blueView mas_updateConstraints:^(MASConstraintMaker *make) { }];
3、約束類型
尺寸:width(寬)\height(高)\size(大小)
// 寬度約束 make.width.mas_equalTo(100); // 高度約束 make.height.mas_equalTo(100); // 大小約束(與下面兩句等價) make.size.mas_equalTo(CGSizeMake(100, 100));// 右邊(leading類似) make.left.mas_equalTo(self.view).offset(50); // 左邊(trailing類似) make.right.equalTo(self.view).offset(-20); // 頂部 make.top.equalTo(self.view).offset(20); // 底部 make.bottom.mas_equalTo(self.view).offset(-50);
// 居中(程度+垂直) // 尺寸是父控件的一半 [blueView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(self.view).multipliedBy(0.5); make.center.mas_equalTo(self.view); // 與上面兩句代碼等價 // make.centerX.mas_equalTo(self.view); // make.centerY.mas_equalTo(self.view); }];// UIEdgeInsets 內邊距 make.edges.mas_equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));
4、mas_前綴修飾與不修飾的區別
mas_equalTo和equalTo默許狀況下:
mas_equalTo有自動包裝功用,比方自動將20包裝為@20
equalTo沒有自動包裝功用
mas_equalTo的功用強於 > equalTo,可以不斷運用mas_equalTo
mas_width和width默許狀況下:
width是make對象的一個屬性,用來添加寬度約束用的,表示對寬度停止約束
mas_width是一個屬性值,用來當做equalTo的參數,表示某個控件的寬度屬性
mas_height、mas_centerX以此類推
消弭區別方法假如添加了上面的宏,那麼 mas_equalTo 和 equalTo 就沒有區別
#define MAS_SHORTHAND_GLOBALS// 留意:這個宏一定要添加到#import "Masonry.h"後面
假如添加了上面的宏,mas_width也可以寫成width
#define MAS_SHORTHAND
//define this constant if you want to use Masonry without the 'mas_' prefix #define MAS_SHORTHAND //define this constant if you want to enable auto-boxing for default syntax #define MAS_SHORTHAND_GLOBALS #import "Masonry.h" - (void)viewDidLoad { [super viewDidLoad]; // 藍色控件 UIView *blueView = [[UIView alloc] init]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:blueView]; // 白色控件 UIView *redView = [[UIView alloc] init]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; // 添加約束 CGFloat margin = 20; CGFloat height = 50; [blueView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.left).offset(margin); make.right.equalTo(redView.left).offset(-margin); make.bottom.equalTo(self.view.bottom).offset(-margin); make.height.equalTo(height); make.top.equalTo(redView.top); make.bottom.equalTo(redView.bottom); make.width.equalTo(redView.width); }]; [redView makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.view.right).offset(-margin); }]; } - (MASConstraint*)with { return self; }
運用狀況示例代碼
// 尺寸限制:100x100 // 地位:粘著父控件右下角,間距是20 [blueView mas_makeConstraints:^(MASConstraintMaker *make) { // 寬度約束 make.width.equalTo(@100); // 高度約束 make.height.equalTo(@100); // 左邊 make.right.equalTo(self.view.mas_right).with.offset(-20); // 頂部 make.top.equalTo(self.view.mas_top).with.offset(20); }]; - (MASConstraint*)and { return self; }
運用狀況示例代碼
// 尺寸限制:100x100 // 地位:粘著父控件右下角,間距是20 [blueView mas_makeConstraints:^(MASConstraintMaker *make) { // 寬度高度約束 make.width.and.height.mas_equalTo(100); // 左邊 make.right.equalTo(self.view).offset(-20); // 頂部 make.top.equalTo(self.view).offset(20); }]; 【iOS學習之Masonry第三方約束】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!