Why is there no UIHostingView?

In AppKit, there is NSHostingController and NSHostingView. In UIKit, there is only UIHostingController. So what gives?

Sure, you can use UIHostingController, grab its view, and ignore the controller (ie not bother adding it as a child view controller). But it seems reasonable to assume that the omission of UIHostingView is intentional and you need the view controller properly connected to make things work right. But which things?

The problem is that incorporating UIHostingController into a view controller hierarchy can be difficult because you might not know at that point it the code what the parent view controller is (unless you add some otherwise unnecessary contortions). And yet the docs say you're supposed to add the child view controller to its parent before you add the view to the superview.

For example, you could have a situation where your UIView is sandwiched in the hierarchy in between SwiftUI components - both embedding and embedded in SwiftUI. In my experiments this works fine in may ways, but UIViewRepresentable doesn't give you a view controller to be the parent of your UIHostingController. So you're stuck with the consequences of an orphan view controller... whatever they are.
Post not yet marked as solved Up vote post of Uncommon Down vote post of Uncommon
2.5k views
  • I've wondered about this omission also.

  • There is, only it is not a public API. _UIHostingView The underscore prefix means it is not public API. But it works if you try it out. However, I would not recommend using it, because there is a reason it is not public, and you will possibly bury mines in your code if you use it.

Add a Comment