我将展示UIViewController中的SwiftUI视图。
let vc = UIHostingController(rootView: SwiftUIView())
navigationController?.pushViewController(vc, animated: true)
struct SwiftUIView: View {
var body: some View {
VStack {
}
.navigationBarTitle("Title")
}
}
只有在视图出现后,标题才会出现。如何修复?
如果使用UINavigationController
,则需要在UIHostingController
上设置标题以使其立即显示。否则,导航标题将仅在SwiftUI视图渲染后显示。
您可以使用title
设置任何UIViewController
的导航标题
let vc = UIHostingController(rootView: SwiftUIView())
vc.title = "Title"
navigationController?.pushViewController(vc, animated: true)