我正在使用storyboard和
Xcode 6来设计应用程序视图但我面临这个问题:我想为纵向和横向模式分配不同的子视图位置.例如:
从现在开始,我已经通过willRotateToInterfaceOrientation和状态栏以编程方式实现了这一目标,以获得ipad方向.
使用Xcode 6,用于纵向和横向的iPhone布局是不同的,但对于iPad(常规,常规)是相同的.有可能通过限制来实现这些位置吗?
是的,你可以通过约束来做到这一点.
首先,您需要为superview创建约束,而不是最近的视图.相邻视图将改变位置,因此我们不希望约束相对于其他视图.有关如何设置约束的示例,请参阅下面的屏幕截图.
接下来,将您要修改的约束链接到IBOutlets,以便我们可以以编程方式修改它们.对于您的示例,这些将是约束:
@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更新约束常量.再次,使用您的示例,代码看起来像这样:
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) } }