当屏幕方向改变时,可以在前一个视图控制器上转换视图?

我有一个情况viewControllerAviewControllerB到导航堆栈。 当用户旋转屏幕和viewControllerB的方向改变,我希望subviewA的子viewControllerA转换和重新定位自己。 这可能吗?

我已经尝试发送一个subviewA的指针viewControllerB ,但我所做的任何关于它的位置被忽略(当viewControllerB当前在屏幕上)。

我也试着在subviewAviewDidAppear中重新定位subviewA ,但是它看起来很丑,因为它在屏幕上快速重新定位。 也尝试在viewWillAppear操纵它,但没有任何反应。

我唯一能想到的是将viewControllerA的整个viewControllerB发送给viewControllerB并在viewControllerB旋转后将其附加为子视图,然后将其删除。 但显然这是一个可怕的解决scheme。

有没有办法做到这一点?

限制可以以这样的方式进行,即当超视图边界改变时,视图的位置和大小可以自动改变(不检查方向)。 如果以这种方式进行约束,那么无论视图在旋转时是否在屏幕上,视图都将具有适当的旋转布局。

约束的forms为y = m*x + b其中y和x是两个视图,m是乘数,b是常数。 它需要做一些math计算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中有任何约束设置将被replace,您可以将它们标记为将在运行时删除的占位符。 类别看起来像这样,

 +(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