可以在屏幕方向更改时转换先前视图控制器上的视图? [英] Possible to transform view on previous view controller when screen orientation changes?

查看:56
本文介绍了可以在屏幕方向更改时转换先前视图控制器上的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况, viewControllerA viewControllerB 推到导航堆栈。当用户旋转屏幕和 viewControllerB 的方向更改时,我想要 subviewA viewControllerA 来转换和重新定位本身。这可能吗?

I have a situation where viewControllerA pushes viewControllerB onto the navigation stack. When the user rotates the screen and the orientation of viewControllerB changes, I'd like for a subviewA of viewControllerA to transform and reposition itself. Is this possible?

我已尝试将 subviewA 的指针传送到 viewControllerB ,但是我对它的位置做的任何事情都被忽略了(当 viewControllerB 当前在屏幕上)。

I've tried sending a pointer of subviewA to viewControllerB, but anything I do with it regarding its position gets ignored (when viewControllerB is currently on the screen).

我也尝试在 viewDidAppear subviewA > viewControllerA 之后 viewControllerB 弹出堆栈,但它看起来很丑陋,因为它迅速重新定位本身,因为它来到屏幕上。也尝试在 viewWillAppear 中操作它,但什么也没有发生。

I've also tried repositioning the subviewA in viewDidAppear of viewControllerA after viewControllerB pops off the stack but it looks ugly as it quickly repositions itself as it comes on screen. Also tried manipulating it in viewWillAppear but nothing happens.

我唯一能想到的是发送 viewControllerA 的整个 self。当 viewControllerB 旋转然后操作它时,将 c $ <然后删除它。但是这显然是一个可怕的解决方案。

The only thing I can think of is to send viewControllerA's entire self.view to viewControllerB and attach it as a subview when viewControllerB rotates and then manipulate it, then remove it. But that's a horrible solution obviously.

有办法吗?

推荐答案

约束可以以这样一种方式进行,当超级视图的边界改变时,视图的位置和大小可以自动改变(不检查方向)。如果以这种方式制定约束,那么视图将在旋转时具有适当的排列,而不管在旋转时视图是否在屏幕上。

Constraints can be made in such a way that the position and size of views can change automatically (no checking on the orientation) when the bounds of the superview change. If you make the constraints this way, then the view will have the proper arrangement on rotation regardless of whether the view was on screen at the time of the rotation or not.

约束具有 y = m * x + b 的形式,其中y和x是两个视图,m是乘数,b是常数。它需要做一点数学计算出m和b的值将给你所需的约束。我做了一个类NSLayoutConstraint允许你直接指定你想要的纵向和横向的值。您可以像这样使用它,

Constraints have the form y = m*x + b where y and x are two views, m is the multiplier, and b is the constant. It requires doing a bit of math to figure out what values of m and b will give you the constraints you want. I've made a category on NSLayoutConstraint that allows you to directly specify what values you want for portrait and landscape. You can use it like this,

- (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中有任何约束设置,可以将它们标记为将在运行时删除的占位符。类别如下:

If you have any constraints setup in IB that would be replaced by these, you can mark them as placeholders that will be removed at runtime. The category looks like this,

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

这篇关于可以在屏幕方向更改时转换先前视图控制器上的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆