Skip to content

Instantly share code, notes, and snippets.

@pyanfield
Created March 26, 2014 06:17
Show Gist options
  • Save pyanfield/9777699 to your computer and use it in GitHub Desktop.
Save pyanfield/9777699 to your computer and use it in GitHub Desktop.
动态调整Top Space
/*
在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