iOS: 在代码中使用Autolayout (1) - 按比例缩放和优先级
首先說按比例縮放,這是在Interface Builder中無法設置的內容。而在代碼中,使用NSLayoutConstraint類型的初始化函數中的multiplier參數就可以非常簡單的設置按比例縮放。同時也可以設置不同NSLayoutAttribute參數來達到意想不到的效果,比如“A的Width等于B的Height的2倍”這樣的效果。
OK,開始寫代碼,我們就拿一個簡單的UIButton做示例,在ViewController中創建一個UIButton字段:
UIButton?*btn;
命令這個Button水平居中,始終距離父View底部20單位的距離。然后高度是父View高度的三分之一。
最后使用KVO來監控Button的大小并實時輸出到屏幕上。
代碼:
- (void)viewDidLoad
{
??? [super?viewDidLoad];
???
????//創建UIButton,不需要設置frame
????btn?= [UIButton?buttonWithType:UIButtonTypeRoundedRect];
??? [btn?setTitle:@"mgen"?forState:UIControlStateNormal];
????btn.backgroundColor?= [UIColor?greenColor];
??? [self.view?addSubview:btn];
????//禁止自動轉換AutoresizingMask
????btn.translatesAutoresizingMaskIntoConstraints?=?NO;
???
????//居中
??? [self.view?addConstraint:[NSLayoutConstraint
??????????????????????????????constraintWithItem:btn
??????????????????????????????attribute:NSLayoutAttributeCenterX
??????????????????????????????relatedBy:NSLayoutRelationEqual
??????????????????????????????toItem:self.view
??????????????????????????????attribute:NSLayoutAttributeCenterX
??????????????????????????????multiplier:1
??????????????????????????????constant:0]];
???
????//距離底部20單位
????//注意NSLayoutConstraint創建的constant是加在toItem參數的,所以需要-20。
??? [self.view?addConstraint:[NSLayoutConstraint
??????????????????????????????constraintWithItem:btn
??????????????????????????????attribute:NSLayoutAttributeBottom
??????????????????????????????relatedBy:NSLayoutRelationEqual
??????????????????????????????toItem:self.view
??????????????????????????????attribute:NSLayoutAttributeBottom
??????????????????????????????multiplier:1
??????????????????????????????constant:-20]];
???
????//定義高度是父View的三分之一
??? [self.view?addConstraint:[NSLayoutConstraint
??????????????????????????????constraintWithItem:btn
??????????????????????????????attribute:NSLayoutAttributeHeight
??????????????????????????????relatedBy:NSLayoutRelationEqual
??????????????????????????????toItem:self.view
??????????????????????????????attribute:NSLayoutAttributeHeight
??????????????????????????????multiplier:0.3
??????????????????????????????constant:0]];
???
????//注冊KVO方法
??? [btn?addObserver:self?forKeyPath:@"bounds"?options:NSKeyValueObservingOptionNew?|NSKeyValueObservingOptionInitial?context:nil];???
}
?
//KVO回調
- (void)observeValueForKeyPath:(NSString?*)keyPath ofObject:(id)object change:(NSDictionary?*)change context:(void?*)context
{
????if?(object ==?btn?&& [keyPath?isEqualToString:@"bounds"])
??? {
??????? [btn?setTitle:NSStringFromCGSize(btn.bounds.size)?forState:UIControlStateNormal];
??? }
}
?
運行結果:
?
?
OK,沒有任何問題。
?
接下來有一個新的需求,在橫向的顯示中,Button的高度只有96,覺得他太短了,所以要求Button的最小高度為150。
這樣的話,需要加入另一個限制大小的Constraint,但是這兩個Constraint在某些情況下是有沖突的,我們可以通過設置Constraint的優先級來解決。優先級對應NSLayoutConstraint類型的priority屬性,默認值是UILayoutPriorityRequired,數值上等于1000. 設置一個低的值代表更低的優先級。
另外對于最小值的定義,使用NSLayoutRelationGreaterThanOrEqual作為NSLayoutConstraint類型創建時的relatedBy參數。
?
修改上面的比例Constraint,并在下方加入一個新的限制最小值的Constraint,代碼:
//定義高度是父View的三分之一
//設置優先級低于UILayoutPriorityRequired(1000),UILayoutPriorityDefaultHigh是750
NSLayoutConstraint?*con = [NSLayoutConstraint
??????????????????????????constraintWithItem:btn
??????????????????????????attribute:NSLayoutAttributeHeight
??????????????????????????relatedBy:NSLayoutRelationEqual
??????????????????????????toItem:self.view
??????????????????????????attribute:NSLayoutAttributeHeight
??????????????????????????multiplier:0.3
??????????????????????????constant:0];
con.priority?=?UILayoutPriorityDefaultHigh;
[self.view?addConstraint:con];
?
//設置btn最小高度為150
[btn?addConstraint:[NSLayoutConstraint
????????????????????constraintWithItem:btn
????????????????????attribute:NSLayoutAttributeHeight
????????????????????relatedBy:NSLayoutRelationGreaterThanOrEqual
????????????????????toItem:nil
????????????????????attribute:NSLayoutAttributeNotAnAttribute
????????????????????multiplier:1
????????????????????constant:150]];
?
運行后,橫向屏幕中的Button高度成了150:
?
總結
以上是生活随笔為你收集整理的iOS: 在代码中使用Autolayout (1) - 按比例缩放和优先级的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Capturing 'self' str
- 下一篇: iOS: 在代码中使用Autolayou