AutoLayout優(yōu)先級(jí),實(shí)現(xiàn)按鈕的隱藏顯示
效果圖:

效果圖.gif
優(yōu)先級(jí)(priority):
- 初始化時(shí),當(dāng)約束的priority的值是UILayoutPriorityRequired (1000)時(shí),是無法改變priority值而生效的。也就是說,想要使用優(yōu)先級(jí)屬性,就必須初始化一個(gè)不是1000的priority的約束。
- storyboar中,手動(dòng)拉線的約束默認(rèn)為UILayoutPriorityRequired (1000)??梢允謩?dòng)修改該屬性。
Priorities may not change from nonrequired to required, or from required to nonrequired.
實(shí)現(xiàn):
在一個(gè)UIView中放置3個(gè)按鈕,用優(yōu)先級(jí)達(dá)到隱藏/顯示其中一個(gè)按鈕的目的。
在storyboard中的約束:

第三個(gè)按鈕的約束

第二個(gè)按鈕的約束
代碼實(shí)現(xiàn)隱藏和顯示
//隱藏-顯示第二個(gè)按鈕
- (IBAction)clickToHidenOrShowSecondBtn:(id)sender {
_isHidenSecondBtn = !_isHidenSecondBtn;
if (_isHidenSecondBtn) {
self.secondBtn.hidden = YES;
self.threeBtnToFirstBtnConstraint.priority = 999;
self.threeBtnToLabelConstraint.priority = 750;
self.threeBtnToSecondBtnConstraint.priority = 750;
} else {
self.secondBtn.hidden = NO;
self.threeBtnToFirstBtnConstraint.priority = 750;
self.threeBtnToLabelConstraint.priority = 750;
self.threeBtnToSecondBtnConstraint.priority = 999;
}
}