Reputation: 5085
For iPad, trait constraints are same for landscape and portrait mode. That is regular width and regular height for both?
How to set separate constraints for both mode, I need layout as shown in the screenshot below:
Landscape mode:
Upvotes: 0
Views: 86
Reputation: 14040
You can't handle that situation in interface builder only. I recommend you to use a UIStackView
and change its axis on orientation change in code.
Maybe you also want to set up and activate / deactivate some width and height constraints for your red view to keep its correct size depending on the current orientation.
A solution could look like this:
@IBOutlet weak var outerStackView: UIStackView!
@IBOutlet weak var redViewWidthConstraint: NSLayoutConstraint!
@IBOutlet weak var redViewHeightConstraint: NSLayoutConstraint!
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
let willTransitionToLandscape = (size.width > size.height)
if willTransitionToLandscape {
redViewHeightConstraint.isActive = false
redViewWidthConstraint.isActive = true
} else {
redViewWidthConstraint.isActive = false
redViewHeightConstraint.isActive = true
}
coordinator.animate(alongsideTransition: { _ in
self.outerStackView.axis = willTransitionToLandscape ? .horizontal : .vertical
self.view.layoutIfNeeded()
})
}
Upvotes: 1