I've got two views 'View A' and 'View B'. I'd like to display them side by side in landscape mode but one over the other in portrait mode. How can I achieve this using Auto Layout?
With the new UIStackView
(introduced in iOS 9.0) it is now easily possible. Just add a UIStackView in the storyboard with your two views and define the desired proportion.
Then in the ViewController you can adapt the alignment of the axis directly on orientation change:
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
switch UIDevice.currentDevice().orientation {
case .Portrait:
self.stackview.axis = .Vertical
case .PortraitUpsideDown:
self.stackview.axis = .Vertical
case .LandscapeLeft:
self.stackview.axis = .Horizontal
case .LandscapeRight:
self.stackview.axis = .Horizontal
default:
self.stackview.axis = .Vertical
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With