- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
// 定義這個(gè)常量,就可以不用在開發(fā)過程中使用"mas_"前綴。
#define MAS_SHORTHAND
// 定義這個(gè)常量,就可以讓Masonry幫我們自動(dòng)把基礎(chǔ)數(shù)據(jù)類型的數(shù)據(jù),自動(dòng)裝箱為對(duì)象類型。
#define MAS_SHORTHAND_GLOBALS
// #import "Masonry.h" 這個(gè)頭文件一定要放在上面兩個(gè)宏的后面
mas_makeConstraints 這個(gè)方法只會(huì)添加新的約束
mas_updateConstraints 這個(gè)方法將會(huì)覆蓋以前的某些特定的約束(更新約束)
mas_remakeConstraints 這個(gè)方法會(huì)將以前的所有約束刪掉,添加新的約束
例:
//viewDidLoad
[_view2 mas_makeConstraints:^(MASConstraintMaker *make) {
__strong typeof(weakSelf) strongSelf = weakSelf;
make.center.equalTo(strongSelf.view);
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_view2 mas_updateConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(150, 150));
}];
}
//viewDidLoad
[_view2 mas_makeConstraints:^(MASConstraintMaker *make) {
__strong typeof(weakSelf) strongSelf = weakSelf;
make.center.equalTo(strongSelf.view);
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_view2 mas_remakeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(150, 150));
}];
}
//由于mas_remakeConstraints清除之前的所有約束,所以view2的x,y沒有約束了,出現(xiàn)在(0,0)位置上
設(shè)置約束優(yōu)先級(jí)
UIView *redView = [[UIView alloc]init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
UIView *greenView = [[UIView alloc]init];
greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];
UIView *blueView = [[UIView alloc]init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
//在使用Masonry添加約束之前,需要在addSubview之后才能使用,否則會(huì)導(dǎo)致崩潰。
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_left).offset(20);
make.bottom.equalTo(self.view.mas_bottom).offset(-20);
make.width.equalTo(self.view.mas_width).multipliedBy(0.2);
make.height.equalTo(self.view.mas_height).multipliedBy(0.2);
}];
[greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(redView.mas_right).offset(20);
make.bottom.equalTo(self.view.mas_bottom).offset(-20);
make.width.equalTo(self.view.mas_width).multipliedBy(0.2);
make.height.equalTo(self.view.mas_height).multipliedBy(0.2);
}];
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(greenView.mas_right).offset(20);
make.bottom.equalTo(self.view.mas_bottom).offset(-20);
make.width.equalTo(self.view.mas_width).multipliedBy(0.2);
make.height.equalTo(self.view.mas_height).multipliedBy(0.2);
make.left.equalTo(redView.mas_right).offset(20).priority(250);
//make.left.equalTo(redView.mas_right).offset(20).priorityLow();
//Masonry為我們提供了三個(gè)默認(rèn)的方法,priorityLow()、priorityMedium()、priorityHigh(),這三個(gè)方法內(nèi)部對(duì)應(yīng)著不同的默認(rèn)優(yōu)先級(jí)。
//除了這三個(gè)方法,我們也可以自己設(shè)置優(yōu)先級(jí)的值,可以通過priority()方法來(lái)設(shè)置。
//優(yōu)先級(jí)的范圍是0~1000,數(shù)字越大,優(yōu)先級(jí)越高,在不設(shè)置的情況下默認(rèn)為1000
//最后添加的這個(gè)約束的優(yōu)先級(jí)是低的,這個(gè)約束只有在它的沖突約束被抹掉后,它才能實(shí)現(xiàn)
}];
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.greenView removeFromSuperview];
[UIView animateWithDuration:1.0f animations:^{
[self.view layoutIfNeeded];
}];
}
大于等于和小于等于某個(gè)值的約束
[self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
// 設(shè)置寬度小于等于200
make.width.lessThanOrEqualTo(@200);
// 設(shè)置高度大于等于10
make.height.greaterThanOrEqualTo(@(10));
}];
設(shè)置約束比例
// 設(shè)置當(dāng)前約束值乘以多少,例如這個(gè)例子是redView的寬度是self.view寬度的0.2倍。
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.height.mas_equalTo(30);
make.width.equalTo(self.view).multipliedBy(0.2);
}];
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。