約束
,參照
frame
約束
,最終仍是爲了肯定其位置
與尺寸
位置
,尺寸
這兩個必要條件優先級
的約束優先級
會在例子中說明其用處
先添加四個Viewphp
UIView *redView = [[UIView alloc]init]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; UIView *blueView = [[UIView alloc]init]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:blueView]; UIView *yellow = [[UIView alloc]init]; yellow.backgroundColor = [UIColor yellowColor]; [self.view addSubview:yellow]; UIView *green = [[UIView alloc]init]; green.backgroundColor = [UIColor greenColor]; [self.view addSubview:green];
mas_makeConstraints
這個方法使左邊等於self.view的左邊,間距爲0
and
與with
其實就是get調用者自己,裏面僅僅是return self[redView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.mas_left).offset(0);//使左邊等於self.view的左邊,間距爲0 make.top.equalTo(self.view.mas_top).offset(0);//使頂部與self.view的間距爲0 make.width.equalTo(self.view.mas_width).multipliedBy(0.5);//設置寬度爲self.view的一半,multipliedBy是倍數的意思,也就是,使寬度等於self.view寬度的0.5倍 make.height.equalTo(self.view.mas_height).multipliedBy(0.5);//設置高度爲self.view高度的一半 }];
錨點
,來添加約束,肯定自身的位置
與尺寸
圖1
中的blueView的效果,咱們應當怎樣添加約束呢?[blueView mas_makeConstraints:^(MASConstraintMaker *make) { make.width.and.height.equalTo(redView);//使寬高等於redView make.top.equalTo(redView.mas_top);//與redView頂部對齊 make.leading.equalTo(redView.mas_right);//與redView的間距爲0 }];
位置
與尺寸
[yellow mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(redView);//與redView左對齊 make.top.equalTo(redView.mas_bottom);//與redView底部間距爲0 make.width.and.height.equalTo(redView);//與redView寬高相等 }]; [green mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(yellow.mas_right);//與yellow右邊間距爲0 make.top.equalTo(blueView.mas_bottom);//與blueView底部間距爲0 make.width.and.height.equalTo(redView);//與redView等寬高 }];
尺寸
的例子,或者是固定位置
的例子,我怕誤導你們認爲Autolayout是很是死板的,必須把每一個控件的約束添加到知足位置
與尺寸
,再去添加其餘控件的約束,這樣纔不會出錯位置
與尺寸
,可是有時這兩個必須條件能夠利用相對
來知足
相對
概念//代碼中View的順序與圖中從左到右的View的順序一致 //例子中,惟一不肯定的就是灰色View的寬度,咱們先把肯定的約束給一個一個的添加上來 //灰1左間距、高度、垂直位置(由於和紅1底部對齊)是肯定的,添加約束 [gray1 mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(20); make.leading.equalTo(self.view.mas_leading).offset(0); make.bottom.equalTo(red1.mas_bottom); }]; //紅1,寬高、左間距、底間距是肯定的,添加約束 [red1 mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(100); make.height.mas_equalTo(50); make.left.equalTo(gray1.mas_right); make.bottom.equalTo(self.view.mas_bottom).offset(-50); }]; //灰2,左間距、高度、垂直位置是肯定的,寬度要與灰1一致,是爲了能均勻填充,添加約束 [gray2 mas_makeConstraints:^(MASConstraintMaker *make) { make.height.and.width.equalTo(gray1); make.leading.equalTo(red1.mas_right); make.bottom.equalTo(red1.mas_bottom); }]; //紅2,寬高、左間距、底間距是肯定的,添加約束 [red2 mas_makeConstraints:^(MASConstraintMaker *make) { make.height.and.width.equalTo(red1); make.leading.equalTo(gray2.mas_right); make.bottom.equalTo(red1.mas_bottom); }]; //灰3,左間距、右間距、高度、垂直位置是肯定的,添加約束 [gray3 mas_makeConstraints:^(MASConstraintMaker *make) { make.height.and.width.equalTo(gray1); make.leading.equalTo(red2.mas_right); make.trailing.equalTo(self.view.mas_right); make.bottom.equalTo(red1.mas_bottom); }];
相對
,紅色方塊寬度是固定的,那麼水平方向上的間距就須要剩下的三個灰色方塊去填充,當界面橫屏時,三個灰色方塊爲了相對
自身寬度要相同,相對
紅色邊界,self.view邊界,間距保持爲0,那麼就得犧牲自身寬度的穩定,去維持這些相對
的約束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]; [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); }];
[self.greenView removeFromSuperview]; [UIView animateWithDuration:1.0f animations:^{ [self.view layoutIfNeeded]; }];