故事板和Swift中不同的纵向/横向视图 [英] Different portrait/landscape views in storyboard and Swift

查看:94
本文介绍了故事板和Swift中不同的纵向/横向视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用情节提要和Xcode 6设计应用程序视图,但我遇到了这个问题:我想为纵向和横向模式分配不同的子视图位置.例如:

I'm using storyboard and Xcode 6 to design app views but I'm facing this issue: I want to assign different positions of subviews for portrait and landscape modes. For example:

从现在开始,我将使用willRotateToInterfaceOrientation和状态栏以编程方式实现此目的,以获取ipad的方向.

Since now I've achieved this programmatically with willRotateToInterfaceOrientation and status bar to get ipad orientation.

对于Xcode 6,纵向和横向的iPhone布局是不同的,但对于iPad(常规,常规)而言,它们是相同的.有可能取得那些职位受到限制吗?

With Xcode 6, iPhone layouts for portrait and landscape are different, but are the same for iPad (regular, regular). It's is possible to achieved those positions with constraints?

推荐答案

是的,您可以在约束条件下执行此操作.

Yes, you can do this with constraints.

首先,您需要创建对超级视图的约束,而不是对最近的视图的约束.相邻的视图将改变位置,因此我们不希望约束相对于其他视图.有关如何设置约束的示例,请参见下面的屏幕截图.

First, you need to create constraints to the superview, not to the nearest view. Neighboring views will be changing positions, so we DO NOT want the constraints to be relative to the other views. See the screenshot below for an example of how to set the constraints.

接下来,将要修改的约束链接到IBOutlets,以便我们可以通过编程方式修改它们.对于您的示例,这些将​​是约束:

Next, link the constraints you'll be modifying to IBOutlets so we can modify them programmatically. For your example, these would be the constraints:

@IBOutlet var greenViewTrailingConstraint: NSLayoutConstraint!
@IBOutlet var greenViewBottomConstraint: NSLayoutConstraint!

@IBOutlet var redViewTopConstraint: NSLayoutConstraint!
@IBOutlet var redViewLeadingConstraint: NSLayoutConstraint!
@IBOutlet var redViewBottomConstraint: NSLayoutConstraint!

@IBOutlet var blueViewTrailingConstraint: NSLayoutConstraint!
@IBOutlet var blueViewTopConstraint: NSLayoutConstraint!
@IBOutlet var blueViewLeadingConstraint: NSLayoutConstraint!

最后,根据UIInterfaceOrientation更新约束常量.同样,使用您的示例,代码看起来像这样:

Finally, update the constraint constants based on UIInterfaceOrientation. Again, using your example, the code looks something like this:

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    let padding: CGFloat = 16.0

    // since we're calling this before the rotation, the height and width are swapped
    let viewHeight = self.view.frame.size.width
    let viewWidth = self.view.frame.size.height

    // if landscape
    if UIInterfaceOrientationIsLandscape(toInterfaceOrientation) {
        greenViewTrailingConstraint.constant = (viewWidth/2.0) + (padding/2.0)
        greenViewBottomConstraint.constant = padding

        blueViewTopConstraint.constant = (viewHeight/2.0) + (padding/2.0)
        blueViewTrailingConstraint.constant = padding
        blueViewLeadingConstraint.constant = (viewWidth/2.0) + (padding/2.0)

        redViewTopConstraint.constant = padding
        redViewBottomConstraint.constant = (viewHeight/2.0) + (padding/2.0)
        redViewLeadingConstraint.constant = (viewWidth/2.0) + (padding/2.0)
    } else { // else portrait
        greenViewBottomConstraint.constant = (viewHeight/2.0) + (padding/2.0)
        greenViewTrailingConstraint.constant = padding

        blueViewTopConstraint.constant = (viewHeight/2.0) + (padding/2.0)
        blueViewTrailingConstraint.constant = (viewWidth/2.0) + (padding/2.0)
        blueViewLeadingConstraint.constant = padding

        redViewLeadingConstraint.constant = (viewWidth/2.0) + (padding/2.0)
        redViewBottomConstraint.constant = padding
        redViewTopConstraint.constant = (viewHeight/2.0) + (padding/2.0)
    }
}

这篇关于故事板和Swift中不同的纵向/横向视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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