When hosting a SwiftUI view in UIHostingController, the view's safe area is being ignored
how can I fix that?
Hosting View Controller
import SwiftUI
import UIKit
class ViewController: UIViewController {
private var hosting: UIHostingController<SwiftUIView>!
override func viewDidLoad() {
super.viewDidLoad()
hosting = .init(rootView: SwiftUIView())
view.addSubview(hosting.view)
hosting.view.frame = view.bounds
}
}
SwiftUI View
import SwiftUI
struct SwiftUIView: View {
var body: some View {
VStack {
Text("Hello, World!")
.font(.title)
.foregroundColor(.white)
Spacer(minLength: 0)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.red)
}
}

It's not the right way to use a SwiftUI view in a UIKit controller. Actually, the
UIHostingControlleris a controller and it needs to be added as aChildcontroller instead of just including the view itself.