關於AutoLayout,最先從iOS6開始引入使用。html
AutoLayout是一種基於約束的,描述性的佈局系統。 Auto Layout Is a Constraint-Based, Descriptive Layout System.
使用約束條件來描述佈局,view的frame會依據這些約束來進行計算 Describe the layout with constraints, and frames are calculated automatically.
Autoresizing Mask是AutoLayout的子集,任何能夠用Autoresizing Mask完成的工做均可以用AutoLayout完成。AutoLayout還具有一些Autoresizing Mask不具有的優良特性,以幫助咱們更方便地構建界面。
1 |
[NSLayoutConstraint constraintWithItem:button |
2 |
attribute:NSLayoutAttributeBottom |
3 |
relatedBy:NSLayoutRelationEqua |
4 |
toItem:superview |
5 |
attribute:NSLayoutAttributeBottom |
6 |
multiplier:1.0 |
7 |
constant:-padding] |
1 |
[NSLayoutConstraint constraintsWithVisualFormat:@\\ "[cancelButton]-[acceptButton]\" |
2 |
options:0 |
3 |
metrics:nil |
4 |
views:viewsDictionary]; |
1 |
UIButton *cancelButton = ... |
2 |
UIButton *acceptButton = ... |
3 |
viewsDictionary = NSDictionaryOfVariableBindings(cancelButton,acceptButton); |
{ acceptButton = ""; cancelButton = ""; }固然,不嫌累的話本身手寫也何嘗不可。如今字典啊數組啊寫法相對簡化了不少了,所以也不復雜。關於Objective-C的新語法,能夠參考我以前的一篇WWDC 2012筆記: WWDC 2012 Session筆記——405 Modern Objective-C。 在view名字後面添加括號以及鏈接處的數字能夠賦予表達式更多意義,如下進行一些舉例:
1 |
[UIView animateWithDuration:0.5 animations:^{ |
2 |
[view layoutIfNeeded]; |
3 |
}]; |
//-----autoLayoutgit
[_btn_1 setTranslatesAutoresizingMaskIntoConstraints:NO];github
[_btn_2 setTranslatesAutoresizingMaskIntoConstraints:NO];objective-c
[_btn_3 setTranslatesAutoresizingMaskIntoConstraints:NO];數組
[_btn_4 setTranslatesAutoresizingMaskIntoConstraints:NO];app
CGSize winSize = [[iHappySDKSingle shareSingle] getScreenSize];ide
CGFloat tpo = _btn_1.frame.origin.y;函數
CGFloat hpod = _btn_1.frame.origin.x;佈局
CGFloat btnH = _btn_1.frame.size.height;動畫
CGFloat vpod = winSize.width*0.15-btnH;
NSNumber* tp = [NSNumber numberWithFloat:tpo];
NSNumber* hd = [NSNumber numberWithFloat:hpod];
NSNumber* vd = [NSNumber numberWithFloat:vpod];
NSNumber* bh = [NSNumber numberWithFloat:btnH];
NSNumber* btm = [NSNumber numberWithFloat:vpod*2];
NSDictionary *dict1 = NSDictionaryOfVariableBindings(_btn_1,_btn_2,_btn_3,_btn_4);
NSDictionary *metrics =@{@"hPadding":hd,@"vPadding":vd,@"top":tp,@"btm":btm,@"btnHeight":bh};
NSString *vfl1 = @"|-hPadding-[_btn_1]-hPadding-|";
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl1
options:0
metrics:metrics
views:dict1]];
NSString *vfl2 = @"|-hPadding-[_btn_2]-hPadding-|";
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl2
options:0
metrics:metrics
views:dict1]];
NSString *vfl3 = @"|-hPadding-[_btn_3]-hPadding-|";
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl3
options:0
metrics:metrics
views:dict1]];
NSString *vfl4 = @"|-hPadding-[_btn_4]-hPadding-|";
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl4
options:0
metrics:metrics
views:dict1]];
NSString *vfl5 = @"V:|-(<=top)-[_btn_1(btnHeight)]-vPadding-[_btn_2(btnHeight)]-vPadding-[_btn_3(btnHeight)]-vPadding-[_btn_4(btnHeight)]-(>=btm)-|";
if (_btn_1.hidden) {
vfl5 = @"V:|-(<=top)-[_btn_2(btnHeight)]-vPadding-[_btn_3(btnHeight)]-vPadding-[_btn_4(btnHeight)]-(>=btm)-|";
}
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl5
options:0
metrics:metrics
views:dict1]];
- (void)setAutoLayoutForKuang:(UIView*)imgv
{
UIView * view = self;
[imgv setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *dict1 = NSDictionaryOfVariableBindings(imgv);
NSDictionary *metrics = @{@"width":[NSNumbernumberWithFloat:imgv.frame.size.width],
@"height":[NSNumber numberWithFloat:imgv.frame.size.height],
@"top":[NSNumber numberWithFloat:imgv.frame.origin.y]
};
NSString *vfl1 = @"[imgv(width)]";
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl1
options:0
metrics:metrics
views:dict1]];
NSString *vfl2 = @"V:[imgv(height)]";
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl2
options:0
metrics:metrics
views:dict1]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:imgvattribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:viewattribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:imgvattribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:viewattribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
//animation
[UIView animateWithDuration:0.25 animations:^{
[imgv layoutIfNeeded];
}];
}