Created
March 26, 2014 06:17
-
-
Save pyanfield/9777699 to your computer and use it in GitHub Desktop.
动态调整Top Space
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
在nib文件中设置autolayout,设置 parentView 和 childView 之间的 top space,然后根据横竖屏来调整 top space大小 | |
*/ | |
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration | |
{ | |
if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){ | |
[self adjustTopSpace:100.0f]; | |
}else{ | |
[self adjustTopSpace:300.0f]; | |
} | |
} | |
- (void)adjustTopSpace:(CGFloat)contant | |
{ | |
NSArray *constraints = [parentView.constraints copy]; | |
for (NSLayoutConstraint *constraint in constraints) { | |
id firstItem = constraint.firstItem; | |
NSLayoutAttribute fa = constraint.firstAttribute; | |
id secondItem = constraint.secondItem; | |
NSLayoutAttribute sa = constraint.secondAttribute; | |
NSLayoutRelation rl = constraint.relation; | |
CGFloat mp = constraint.multiplier; | |
CGFloat c = constraint.constant; | |
Logger(@"======begin======= "); | |
Logger(@"first item: %@",firstItem); | |
Logger(@"second item: %@",secondItem); | |
Logger(@"first attri: %ld",fa); | |
Logger(@"second attri: %ld",sa); | |
Logger(@"multiplier : %f",mp); | |
Logger(@"constant: %f",c); | |
Logger(@"relation: %ld",rl); | |
Logger(@"=======end======= "); | |
if (childView == firstItem && fa == NSLayoutAttributeTop && parentView == secondItem && sa == NSLayoutAttributeTop) { | |
NSLayoutConstraint *fixedConstraint = [NSLayoutConstraint constraintWithItem:firstItem attribute:constraint.firstAttribute relatedBy:constraint.relation toItem:secondItem attribute:constraint.secondAttribute multiplier:constraint.multiplier constant:contant]; | |
[parentView removeConstraint:constraint]; | |
[parentView addConstraint:fixedConstraint]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment