AutoLayout优先级,实现按钮的隐藏显示
效果图:
优先级(priority):
- 初始化时,当约束的priority的值是UILayoutPriorityRequired (1000)时,是无法改变priority值而生效的。也就是说,想要使用优先级属性,就必须初始化一个不是1000的priority的约束。
- storyboar中,手动拉线的约束默认为UILayoutPriorityRequired (1000)。可以手动修改该属性。
Priorities may not change from nonrequired to required, or from required to nonrequired.
实现:
在一个UIView中放置3个按钮,用优先级达到隐藏/显示其中一个按钮的目的。
在storyboard中的约束:
代码实现隐藏和显示
//隐藏-显示第二个按钮
- (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;
}
}