SwifUI: support only portrait orientation on view?

Viewed 116

I'm presenting a modal SwiftUI view:

let vc = UIHostingController(rootView: MaModalView(title: "TEST"))
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true)

I do not want this modal to rotate, I want it to only work in portrait mode.

How do I get a SwiftUI view to only support a given orientation?

1 Answers

Just override supported interface orientation, like

let vc = MyViewController(rootView: MaModalView(title: "TEST"))
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true)

class MyViewController: UIHostingController<MaModalView> {
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
}
Related