Xcode根据屏幕方向改变约束 [英] Xcode changing constraints dependant on screen orientation

查看:196
本文介绍了Xcode根据屏幕方向改变约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have designed an app using auto layout and all works perfectly, issue is, when I switch to landscape I want the location of one button to move to the other side of the screen.

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

Im assuming the best way of doing this is by changing the constraints when I rotate the screen, is this possible in the code, or even the best way of doing it? Obviously if the app starts up in landscape I obviously want this button to be in the landscape mode also.

谢谢

推荐答案

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

Absolutely. Remove and add constraints as desired in your view controller's updateViewConstraints implementation.

您会发现最容易准备两个集合预先设置备用约束,将它们保存在实例变量中。然后你可以将它们交换进去。下面是代码外观的草图:

You will find it easiest to prepare both sets of alternate constraints beforehand, maintaining them in instance variables. Then you can just swap them in and out. Here's a sketch of what the code could look like:

-(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];
}

这篇关于Xcode根据屏幕方向改变约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆