UIHostingViewController背景色显示

时间:2020-03-25 02:37:00

标签: swiftui

我正在通过SwiftUI视图显示UIViewController,就像这样:

@available(iOS 13, *)
struct WelcomeNavView: UIViewControllerRepresentable {
    func makeUIViewController(context: UIViewControllerRepresentableContext<WelcomeNavView>) -> UINavigationController {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let navVc = storyboard.instantiateViewController(withIdentifier: "welcomeNav") as! UINavigationController
        return navVc
    }

    func updateUIViewController(_ uiViewController: UINavigationController, context: UIViewControllerRepresentableContext<WelcomeNavView>) {
    }
}

然后我像这样从ViewController呈现它:

self.viewController?.present(style: .fullScreen) {
    WelcomeNavView()
}

但是,它不会占据整个屏幕,并且UIHostViewController颜色显示在顶部和底部:

enter image description here

如何更改UIHostingViewController视图的颜色。或展开它所持有的View来占据整个屏幕?

1 个答案:

答案 0 :(得分:0)

这是解决方案, 我遇到了类似的问题,我必须将托管视图控制器的背景设置为清晰。

显然,hostingVC 和 hostsVC.view 的 rootView(可能是我们的 SwiftUI 视图)是不同的。

所以这将允许您更改为背景颜色。

hostingVC.view.backgroundColor = .clear

当然可以用任何颜色代替“.clear”。

请记住相应地更改作为 rootView 传递给 hostsVC 的 SwiftUI 视图的颜色,主要是要清楚这一点,所以它不会在上面设置的颜色上显示。

Your_SwiftUI_View.background(Color.clear)