Home > OS >  SwiftUI: UIHostingController<?> generic?
SwiftUI: UIHostingController<?> generic?

Time:09-29

I am doing this:

class PortraitViewController: UIHostingController<MyView> {
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
}

then when I instantiate it I do this:

let vc = PortraitViewController(rootView: MyView())

All works great.

I would like to create a fully generic PortraitViewController that could accept any View and not just MyView.

I am not sure how to change the UIHostingController<MyView> to make it work with any view. I tried UIHostingController<View> but it's not working.

How can I make it so let vc = PortraitViewController(rootView: MyView()) can work with another View and not just MyView?

CodePudding user response:

You can constrain the generic to just a View type:

class PortraitViewController<T:View>: UIHostingController<T>
  • Related