简体   繁体   English

调整大小并在景观中放置旋转视图:更新约束还是平移/更改框架?

[英]Resizing and placing a rotated view in landscape: updating constraints or translating/changing frames?

I've a subview which I have placed at the bottom of the screen in portrait orientation: 我有一个子视图,它以纵向放置在屏幕底部:

在此处输入图片说明

I've set constraints in storyboard for this orientation to pin its leading, trailing, and bottom space to the superview, as well as a fixed height of the subview. 我在storyboard中为此方向设置了约束,以将其前导,尾随和底部空间固定到超级视图,以及子视图的固定高度。

I want this subview to be at the right side of the screen when the device is in landscape orientation like this: 当设备处于横向时,我希望此子视图位于屏幕的右侧,如下所示:

在此处输入图片说明

In my UIViewController subclass behind, I apply a M_PI/2 radians rotation to the subview in viewWillTransitionToSize:withTransitionCoordinator: , then I get this result: 在后面的UIViewController子类中,将M_PI / 2弧度旋转应用于viewWillTransitionToSize:withTransitionCoordinator:的子视图,然后得到以下结果:

在此处输入图片说明

I tried to place the subview to the right side of the screen as I want, so after the rotation I call [self.view setNeedsUpdateConstraints]; 我尝试根据需要将子视图放置在屏幕的右侧,因此在旋转之后,我调用[self.view setNeedsUpdateConstraints]; and tried to set new constraints for this orientation, but I'm not able to get the result I want. 并尝试为此定位设置新的约束,但是我无法获得想要的结果。 So, I'm not sure if I'm trying to do the wrong thing... I thought that setting new constraints to pin the subview to the right side of the screen, would resize its height and refresh its X and Y values... Or maybe the right way is to translate the rotated subview and change its frame? 因此,我不确定是否要尝试做错事情...我认为,设置新约束将子视图固定在屏幕的右侧,将会调整其高度并刷新其X和Y值。 ..也许正确的方法是平移旋转的子视图并更改其框架?

How could I solve this? 我该如何解决?

Thanks 谢谢

EDIT: This is an example of the constraints I'm programmatically setting for landscape: 编辑:这是我以编程方式为景观设置的约束示例:

NSLayoutConstraint *customSubviewConstraintTop = [NSLayoutConstraint constraintWithItem:self.customSubview
         attribute:NSLayoutAttributeLeading
         relatedBy:NSLayoutRelationEqual
         toItem:self.view
         attribute:NSLayoutAttributeLeading
         multiplier:1.0
         constant:0];

This is supposed to pin the top of the subview (left side before being rotated) to the top of the screen (right side of the screen when it was in portrait)... right? 这应该是将子视图的顶部(旋转前的左侧)固定到屏幕的顶部(纵向时为屏幕的右侧)...对吗?

EDIT 2: This is the code I have for landscape constraints in updateViewConstraints : 编辑2:这是我在updateViewConstraints用于横向约束的updateViewConstraints

if (!isPortrait) {

    self.customSubView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI/2);

    [self.view removeConstraint:self.customSubViewConstraintL];
    [self.view removeConstraint:self.customSubViewConstraintR];
    [self.view removeConstraint:self.customSubViewConstraintB];
    [self.view removeConstraint:self.customSubViewConstraintH];

    [self.customSubView setTranslatesAutoresizingMaskIntoConstraints:NO];

    self.customSubViewConstraintH = [NSLayoutConstraint constraintWithItem:self.customSubView
                                                          attribute:NSLayoutAttributeWidth
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                         multiplier:1.0
                                                           constant:35.0];

    self.customSubViewConstraintL = [NSLayoutConstraint constraintWithItem:self.customSubView
                                                          attribute:NSLayoutAttributeLeading
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeLeading
                                                         multiplier:1.0
                                                           constant:0.0];

    self.customSubViewConstraintR = [NSLayoutConstraint constraintWithItem:self.customSubView
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTrailing
                                                         multiplier:1.0
                                                           constant:0.0];

    self.customSubViewConstraintB = [NSLayoutConstraint constraintWithItem:self.customSubView
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant:0.0];

    [self.view addConstraint:self.customSubViewConstraintH];
    [self.view addConstraint:self.customSubViewConstraintL];
    [self.view addConstraint:self.customSubViewConstraintR];
    [self.view addConstraint:self.customSubViewConstraintB];
}

You probably use screen-bounded constrains, such as BottomLayoutGuide. 您可能使用了受屏幕限制的约束,例如BottomLayoutGuide。 Use vertical spacing instead. 请改用垂直间距。

UPD: The problem is that you set rotation to you custom view in viewWillTransitionToSize:withTransitionCoordinator: . UPD:问题是您在viewWillTransitionToSize:withTransitionCoordinator:中将旋转设置为自定义视图。 Just set the desired height and width instead. 只需设置所需的高度和宽度即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM