Add hosting view controller to UIKit View

Hi All, I am trying to add an UIHostingViewController to my UIViewController, to cover the screen. Unfortunately, I keep getting this error (Thread 1: "NSLayoutConstraint for <_TtGC7SwiftUI19UIHostingControllerV5Pickt10ViewRouter_: 0x7fa32a37c3f0>: Constraint items must each be a view or layout guide.") from my code:

let controller = UIHostingController(rootView: ViewRouter(join: join, create: create, showPicker: showPicker, setMock: setMock))

        controller.view.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(controller.view)

        view.addConstraints([

            NSLayoutConstraint(item: controller,

                               attribute: .top,

                              relatedBy: .equal,

                              toItem: view.safeAreaLayoutGuide,

                              attribute: .top,

                              multiplier: 1,

                              constant: 0),

            NSLayoutConstraint(item: controller,

                               attribute: .leading,

                               relatedBy: .equal,

                               toItem: view.safeAreaLayoutGuide,

                               attribute: .leading,

                               multiplier: 1,

                               constant: 0),

            NSLayoutConstraint(item: controller,

                               attribute: .trailing,

                              relatedBy: .equal,

                              toItem: view.safeAreaLayoutGuide,

                              attribute: .trailing,

                              multiplier: 1,

                              constant: 0),

            NSLayoutConstraint(item: controller,

                               attribute: .bottom,

                              relatedBy: .equal,

                              toItem: view.safeAreaLayoutGuide,

                              attribute: .bottom,

                              multiplier: 1,

                              constant: 0)

          ])

The error is saying that the item properties in an NSLayoutConstraint must either be a view or a layout guide. In your constraints, you're setting the item to be the view controller itself, when in reality you probably want to pass in the view controller's view.

Add hosting view controller to UIKit View
 
 
Q