Xcode根据屏幕方向改变约束

时间:2014-02-01 18:12:24

标签: xcode ipad autolayout

我设计了一个使用自动布局的应用程序,并且所有工作都很完美,问题是,当我切换到横向时,我希望一个按钮的位置移动到屏幕的另一侧。

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

感谢

1 个答案:

答案 0 :(得分:4)

绝对。在视图控制器的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];
}
相关问题