ios - 当屏幕方向改变时,可以在以前的 View Controller 上转换 View 吗?

标签 ios cocoa-touch uiviewcontroller

我遇到这样一种情况,其中 viewControllerAviewControllerB 推送到导航堆栈中。当用户旋转屏幕并且 viewControllerB 的方向发生变化时,我希望 viewControllerAsubviewA 进行转换和重新定位。这可能吗?

我已经尝试将 subviewA 的指针发送到 viewControllerB,但是我对其位置所做的任何操作都会被忽略(当 viewControllerB当前在屏幕上)。

我还尝试在 viewControllerB 弹出堆栈后,在 viewControllerAviewDidAppear 中重新定位 subviewA 但是它看起来很难看,因为它在出现在屏幕上时会迅速重新定位。还尝试在 viewWillAppear 中对其进行操作,但没有任何反应。

我唯一能想到的就是将viewControllerA的整个self.view发送给viewControllerB,并在viewControllerB 旋转然后操作它,然后移除它。但这显然是一个糟糕的解决方案。

有办法吗?

最佳答案

可以通过这样一种方式进行约束:当父 View 的边界发生变化时, View 的位置和大小可以自动改变(不检查方向)。如果您以这种方式进行约束,那么无论旋转时 View 是否在屏幕上, View 都会在旋转时进行适当的排列。

约束的形式为 y = m*x + b,其中 y 和 x 是两个 View ,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

关于ios - 当屏幕方向改变时,可以在以前的 View Controller 上转换 View 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24499035/

相关文章:

android - 适用于 Android 和 iOS 的 Unity3D : QR-Code Decoding (Reader)

ios - View Controller 的包装器应该是 View Controller 吗?

javascript - 显示旧 iOS 6 键盘和旧选择器的 PhoneGap 应用程序

ios - 更改应用语言并更新版本

iphone - UIWebView 在横向 View 右侧加载一些黑屏

iphone - 在 iPhone 中实现多个 View

ios - 在目标 iOS 3+ 代码中保留未使用的 iOS5 方法是否安全?

ios - 我如何呈现 'standalone' View Controller ('forgetting' 前一个)

ios - 如果从过流上下文 View Controller 中呈现,则呈现错误的 View Controller

iphone - 带注释的 map 边界框