0
votes

For Ipads with size class regular-regular for both device orientations portrait/landscape I want to force a stack view to be

horizontal if device is in portrait mode
vertical if the device is in landscape mode

Though I can set the stack view orientation in the story board for R:R, I cannot set it depending the portrait/landscape mode. How can I achive this?

1
Listen for the "view will change size" event and use the horizontal/vertical ratio to know which orientation we are in.matt
I've made a few attempts but with no luck. Setting the stack views axis in viewWillLayoutSubviews method doesn't seem to work. What do you mean with the "will change size" event exactly?frameworker

1 Answers

-1
votes

There is a built in UIViewController method which does this. "stack" below is the stack view:

override func traitCollectionDidChange(_ previousTraitCollection: 
UITraitCollection?) {
    if view.traitCollection.verticalSizeClass == .compact {
        stack.axis = .horizontal
    } else {
        stack.axis = .vertical
    }
}