在UIKit和SwiftUI混合项目中,导航栏标题出现延迟



我将展示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)

最新更新