zumzum
zumzum

Reputation: 20138

SwiftUI: UIHostingController<?> generic?

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?

Upvotes: 4

Views: 1689

Answers (1)

jnpdx
jnpdx

Reputation: 52367

You can constrain the generic to just a View type:

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

Upvotes: 5

Related Questions