一 簡(jiǎn)單參數(shù)的介紹
>參數(shù)一:(UILayoutPriority)
>設(shè)置優(yōu)先級(jí)等級(jí),數(shù)值越大霹期,優(yōu)先級(jí)越高叶组。
>UILayoutPriorityRequired == 1000;
>UILayoutPriorityDefaultHigh == 750历造;
>UILayoutPriorityDefaultLow == 250甩十;
>UILayoutPriorityFittingSizeLevel == 50船庇;
mas_makeConstraints() 添加約束
mas_remakeConstraints() 移除之前的約束,重新添加新的約束
mas_updateConstraints() 更新約束枣氧,寫哪條更新哪條溢十,其他約束不變
equalTo() 參數(shù)是對(duì)象類型,一般是視圖對(duì)象或者mas_width這樣的坐標(biāo)系對(duì)象
mas_equalTo() 和上面功能相同达吞,參數(shù)可以傳遞基礎(chǔ)數(shù)據(jù)類型對(duì)象张弛,可以理解為比上面的API更強(qiáng)大
width() 用來表示寬度,例如代表view的寬度
mas_width() 用來獲取寬度的值酪劫。和上面的區(qū)別在于吞鸭,一個(gè)代表某個(gè)坐標(biāo)系對(duì)象,一個(gè)用來獲取坐標(biāo)系對(duì)象的值
[_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.mas_left);
make.width.equalTo(weakSelf).multipliedBy(0.01f);//相對(duì)于weakSelf比例進(jìn)行縮放
make.top.equalTo(weakSelf);
make.bottom.equalTo(weakSelf.mas_bottom).mas_offset(-10);
}];
二 動(dòng)態(tài)刷新布局簡(jiǎn)單實(shí)用
- (void)viewDidLoad {
[super viewDidLoad];
//1.添加三個(gè)View
UIView *orangeView = [[UIView alloc]init];
orangeView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:orangeView];
self.orangeView = orangeView;
//2.
UIView *yellowView = [[UIView alloc]init];
yellowView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:yellowView];
self.yellowView = yellowView;
//3.
UIView *greenView = [[UIView alloc]init];
greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];
self.greenView = greenView;
//配置約束
[self setUpRestrain];
}
//配置約束
-(void)setUpRestrain
{
__weak typeof( self) weakSelf = self;
//對(duì)于橙色View只需正常設(shè)置約束就好
[self.orangeView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(100);
make.left.offset(10);
make.top.offset(50);
}];
//黃色View只會(huì)發(fā)生一次變化覆糟,就多設(shè)一個(gè)優(yōu)先級(jí)較低的約束就好
[self.yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(100);
make.left.equalTo(weakSelf.orangeView.mas_right).offset(20);
make.top.offset(50);
//當(dāng)橙色View消失后,黃色View缺少左邊約束刻剥,所以給其加一個(gè)優(yōu)先級(jí)更低的左邊約束。當(dāng)?shù)谝粋€(gè)左邊約束失效后滩字,這個(gè)約束就起作用了
make.left.equalTo(weakSelf.view.mas_left).offset(20).priority(300);
}];
//同理綠色View的低級(jí)約束就得設(shè)置兩個(gè)
[self.greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(100);
make.left.equalTo(weakSelf.yellowView.mas_right).offset(20);
make.top.offset(50);
make.left.equalTo(weakSelf.orangeView.mas_right).offset(20).priority(300);
make.left.equalTo(weakSelf.view.mas_left).offset(20).priority(250);
}];
}
//刪除黃色View
- (IBAction)cancelYellowBtn:(id)sender {
//這里有個(gè)知識(shí)點(diǎn):用約束布局實(shí)現(xiàn)動(dòng)畫造虏,布局代碼寫在外面,然后調(diào)用強(qiáng)制布局方法寫在UIView動(dòng)畫里面
[self.yellowView removeFromSuperview];
[UIView animateWithDuration:0.5 animations:^{
//強(qiáng)制刷新布局
[self.view layoutIfNeeded];
}];
}
//刪除橙色View
- (IBAction)cancelGreenBtn:(id)sender {
[self.orangeView removeFromSuperview];
[UIView animateWithDuration:0.5 animations:^{
//強(qiáng)制刷新布局
[self.view layoutIfNeeded];
}];
}
三 硬度的簡(jiǎn)單實(shí)用
最近在使用Masonry時(shí)出現(xiàn)一個(gè)布局問題麦箍,如下圖:
正常顯示:
布局需求是漓藕,最左側(cè)頭像完整顯示,姓名完整顯示挟裂,性別和年領(lǐng)完整顯示享钞,病名緊貼右側(cè),左側(cè)不能遮擋年齡诀蓉,病名過長(zhǎng)可以不顯示完全栗竖。
開始有問題的布局代碼:
[self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(12.5);
make.size.mas_equalTo(CGSizeMake(45, 45));
make.centerY.equalTo(self.contentView);
}];
[self.lbPatientName mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.headImageView.mas_right).with.offset(7);
make.top.equalTo(self.headImageView).with.offset(2.5);
}];
[self.lbPatientAge mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.lbPatientName.mas_right).offset(10);
make.centerY.equalTo(self.lbPatientName);
}];
[self.btnIllDiagnose mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.lbPatientName);
make.right.equalTo(self.contentView).offset(-15);
make.left.greaterThanOrEqualTo(self.lbPatientAge.mas_right).offset(3);
}];
這樣布局的話當(dāng)病名過長(zhǎng)時(shí),會(huì)出現(xiàn)性別年齡被遮擋的情況渠啤,如上圖一狐肢。
解決辦法:
因?yàn)槲覀儧]有給姓名、病名和性別年齡設(shè)定固定寬度沥曹,所以系統(tǒng)在自動(dòng)布局的時(shí)候并不知道哪個(gè)應(yīng)該被完整顯示份名,哪個(gè)可以不完整顯示。所以我們要給控件設(shè)置布局優(yōu)先級(jí):
修改后代碼如下:
[self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(12.5);
make.size.mas_equalTo(CGSizeMake(45, 45));
make.centerY.equalTo(self.contentView);
}];
[self.lbPatientName mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.headImageView.mas_right).with.offset(7);
make.top.equalTo(self.headImageView).with.offset(2.5);
}];
[self.lbPatientAge mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.lbPatientName.mas_right).offset(10);
make.centerY.equalTo(self.lbPatientName);
}];
[self.btnIllDiagnose mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.lbPatientName);
make.right.equalTo(self.contentView).offset(-15);
make.left.greaterThanOrEqualTo(self.lbPatientAge.mas_right).offset(3);
}];
// 設(shè)置優(yōu)先級(jí)
[self.lbPatientName setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
[self.lbPatientAge setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
[self.btnIllDiagnose setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
設(shè)置優(yōu)先級(jí)方法:
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
第一個(gè)參數(shù)(priority):通俗來講架专,不同的優(yōu)先級(jí)同窘,表示顯示的完整性的高低玄帕,優(yōu)先級(jí)越高部脚,那么在父控件無(wú)法在無(wú)越界的情況下的情況下,就會(huì)優(yōu)先先把優(yōu)先級(jí)高的控件顯示完整裤纹,然后再依次顯示優(yōu)先級(jí)低的
第二個(gè)參數(shù)(axis):代表在什么方向上進(jìn)行優(yōu)先級(jí)限制
這樣顯示就正常了: