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

时间:2014-06-30 21:13:49

标签: ios cocoa-touch uiviewcontroller

我遇到viewControllerAviewControllerB推送到导航堆栈的情况。当用户旋转屏幕并且viewControllerB的方向发生变化时,我希望subviewA viewControllerA转换并重新定位自己。这可能吗?

我尝试向subviewA发送viewControllerB的指针,但我对其位置的任何操作都会被忽略(当viewControllerB当前在屏幕上时)。

我已经尝试在subviewA弹出堆栈之后重新定位viewDidAppear viewControllerA viewControllerB中的viewWillAppear,但它看起来很难看,因为它快速重新定位自身屏幕。还尝试在viewControllerA中操纵它但没有任何反应。

我唯一能想到的是将self.view的整个viewControllerB发送到viewControllerB,并在{{1}}旋转然后操作它时将其作为子视图附加,然后删除它。但这显然是一个可怕的解决方案。

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:2)

可以通过以下方式进行约束:当超视图的边界发生变化时,视图的位置和大小可以自动更改(不检查方向)。如果以这种方式制作约束,则视图将在旋转时具有正确的排列,无论视图是否在旋转时在屏幕上。

约束的形式为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