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