可以通过以下方式进行约束:当超级视图的边界发生变化时,视图的位置和大小可以自动更改(不检查方向)。如果您以这种方式进行约束,那么无论旋转时视图是否在屏幕上,视图都会在旋转时进行正确的排列。
约束的形式为y = m*x + b
其中 y 和 x 是两个视图,m 是乘数,b 是常数。它需要做一些数学计算来找出 m 和 b 的值将为您提供所需的约束。我在 NSLayoutConstraint 上创建了一个类别,允许您直接指定纵向和横向所需的值。你可以像这样使用它,
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addConstraint:[NSLayoutConstraint widthConstraintForView:self.rectangle superview:self.view portraitValue:100 landscapeValue:200]];
[self.view addConstraint:[NSLayoutConstraint heightConstraintForView:self.rectangle superview:self.view portraitValue:150 landscapeValue:80]];
[self.view addConstraint:[NSLayoutConstraint topConstraintForView:self.rectangle viewAttribute:NSLayoutAttributeTop superview:self.view portraitValue:200 landscapeValue:10]];
[self.view addConstraint:[NSLayoutConstraint leftConstraintForView:self.rectangle viewAttribute:NSLayoutAttributeLeft superview:self.view portraitValue:100 landscapeValue:100]];
}
如果您在 IB 中设置了任何将被这些约束替换的约束,则可以将它们标记为将在运行时删除的占位符。该类别看起来像这样,
+(NSLayoutConstraint *)heightConstraintForView:(UIView *)subview superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.height - superview.bounds.size.width);
CGFloat constant = pValue - (superview.bounds.size.height * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeHeight relatedBy:0 toItem:superview attribute:NSLayoutAttributeHeight multiplier:multiplier constant:constant];
NSLog(@"height coeffs: %f %f",multiplier,constant);
return con;
}
+(NSLayoutConstraint *)widthConstraintForView:(UIView *)subview superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.width - superview.bounds.size.height);
CGFloat constant = pValue - (superview.bounds.size.width * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeWidth relatedBy:0 toItem:superview attribute:NSLayoutAttributeWidth multiplier:multiplier constant:constant];
NSLog(@"width coeffs: %f %f",multiplier,constant);
return con;
}
+(NSLayoutConstraint *)leftConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.width - superview.bounds.size.height);
CGFloat constant = pValue - (superview.bounds.size.width * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeRight multiplier:multiplier constant:constant];
NSLog(@"left coeffs: %f %f",multiplier,constant);
return con;
}
+(NSLayoutConstraint *)rightConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (superview.bounds.size.width - pValue - superview.bounds.size.height + lValue)/(superview.bounds.size.width - superview.bounds.size.height);
CGFloat constant = superview.bounds.size.width - pValue - (superview.bounds.size.width * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeRight multiplier:multiplier constant:constant];
NSLog(@"right coeffs: %f %f",multiplier,constant);
return con;
}
+(NSLayoutConstraint *)topConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.height - superview.bounds.size.width);
CGFloat constant = pValue - (superview.bounds.size.height * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeBottom multiplier:multiplier constant:constant];
NSLog(@"top coeffs: %f %f",multiplier,constant);
return con;
}
+(NSLayoutConstraint *)bottomConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (superview.bounds.size.height - pValue - superview.bounds.size.width + lValue)/(superview.bounds.size.height - superview.bounds.size.width);
CGFloat constant = superview.bounds.size.height - pValue - (superview.bounds.size.height * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeBottom multiplier:multiplier constant:constant];
NSLog(@"bottom coeffs: %f %f",multiplier,constant);
return con;
}
我在这里发布了这个示例项目,http://jmp.sh/SYgejs6