【问题标题】:Xcode changing constraints dependant on screen orientationXcode 根据屏幕方向更改约束
【发布时间】:2014-02-25 10:08:55
【问题描述】:

我已经设计了一个使用自动布局的应用程序并且一切正常,问题是,当我切换到横向时,我希望一个按钮的位置移动到屏幕的另一侧。

我假设最好的方法是在我旋转屏幕时更改约束,这在代码中是否可能,甚至是最好的方法?显然,如果应用程序以横向模式启动,我显然希望这个按钮也处于横向模式。

谢谢

【问题讨论】:

    标签: xcode ipad autolayout


    【解决方案1】:

    当然。根据需要在视图控制器的 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];
    }
    

    【讨论】:

    猜你喜欢
    • 2017-02-08
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多