Satish Mavani
Satish Mavani

Reputation: 5085

How to set different frame to same view fo iPad landscape and portrait mode in storyboard?

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:

enter image description here

Landscape mode:

enter image description here

Upvotes: 0

Views: 86

Answers (1)

André Slotta
André Slotta

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

Related Questions