XCODE更改约束取决于屏幕方向



我已经使用自动布局设计了一个应用程序,所有这些都可以完美地工作,问题是,当我切换到景观时,我希望一个按钮的位置移至屏幕的另一侧。<<<<<<<<

im假设做到这一点的最佳方法是通过更改旋转屏幕时的约束,这是在代码中,甚至是最好的方法吗?显然,如果该应用以风景启动,我显然也希望此按钮处于景观模式。

谢谢

绝对。在视图控制器的updateViewConstraints实现中删除并根据需要添加约束。

您最容易事先准备两组替代约束,并在实例变量中维护它们。然后,您可以将它们换成和外出。这是代码外观的草图:

-(void)updateViewConstraints {
    [self.view removeConstraints:self.oneSideConstraints];
    [self.view removeConstraints:self.otherSideConstraints];
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
        [self.view addConstraints:self.oneSideConstraints];
    else
        [self.view addConstraints:self.otherSideConstraints];
    [super updateViewConstraints];
}

最新更新