繁体   English   中英

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

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

我有一个子视图,它以纵向放置在屏幕底部:

在此处输入图片说明

我在storyboard中为此方向设置了约束,以将其前导,尾随和底部空间固定到超级视图,以及子视图的固定高度。

当设备处于横向时,我希望此子视图位于屏幕的右侧,如下所示:

在此处输入图片说明

在后面的UIViewController子类中,将M_PI / 2弧度旋转应用于viewWillTransitionToSize:withTransitionCoordinator:的子视图,然后得到以下结果:

在此处输入图片说明

我尝试根据需要将子视图放置在屏幕的右侧,因此在旋转之后,我调用[self.view setNeedsUpdateConstraints]; 并尝试为此定位设置新的约束,但是我无法获得想要的结果。 因此,我不确定是否要尝试做错事情...我认为,设置新约束将子视图固定在屏幕的右侧,将会调整其高度并刷新其X和Y值。 ..也许正确的方法是平移旋转的子视图并更改其框架?

我该如何解决?

谢谢

编辑:这是我以编程方式为景观设置的约束示例:

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

这应该是将子视图的顶部(旋转前的左侧)固定到屏幕的顶部(纵向时为屏幕的右侧)...对吗?

编辑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];
}

您可能使用了受屏幕限制的约束,例如BottomLayoutGuide。 请改用垂直间距。

UPD:问题是您在viewWillTransitionToSize:withTransitionCoordinator:中将旋转设置为自定义视图。 只需设置所需的高度和宽度即可。

暂无
暂无

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

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