SwiftUI TextField inside UIViewController

Hello everyone, I'm trying to integrate a SwiftUI TextField component into a UIViewController. But any kind of tutorial I've seen, have this implementation:
Code Block
let childView = UIHostingController(rootView: SwiftUIView())
addChild(childView)
childView.view.frame = frame
view.addSubview(childView.view)
childView.didMove(toParent: self)

But, when I do this, my Navigation Bar disappear. But when I remove the "addChild" peace of code, everything works well. Is this a wrong thing to do? Can I remove this part of code?

Replies

Something not clear.

In this code, where is the textField ?
The TextField is a Custom SwiftUI View, with some custom logic. This custom view will be added in a UIViewController but when I do that, my navigation bar disappear.

You'll have to be more precise about the context in which you're placing this code. For instance addChild(_:) is a method on a UIViewController, and not on a UIView. But frame is a property on a UIView, not a UIViewController. So I don't know where you're getting the frame from on the right hand side of the = operator on line 4. If it's the parent view controller's view frame, then... frame is the view's position in its super view's coordinate system, whereas bounds is its position in its own coordinate system. So depending on frame, maybe you're covering it up? But if you're trying to add the hosting controller onto the navigation stack, then you should be using pushViewController(_:_:) not addChild(_:). And then, if you have navigationBarHidden(_:) (which we don't know is not the case from the code you posted) in your swiftUI view, who knows, maybe the hosting controller is setting that property correctly, too? And if your swift view has a navigation view in it, I don't even know how much more could go wrong. But we can't see that from your example. If you have a particular bug in particular code, please construct a minimal example that still causes the bug, and post that, and then we can tell for sure.