SwiftUI - 在 NavigationView 中嵌套 TabView 时不显示导航栏标题 [英] SwiftUI - Navigation bar title not displayed when nesting TabView in NavigationView
本文介绍了SwiftUI - 在 NavigationView 中嵌套 TabView 时不显示导航栏标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于特定于应用程序的原因,我必须在 NavigationView 中嵌套一个 TabView.但是标签项的导航栏标题没有显示,只是一个空的导航栏.
Due to application specific reasons I have to nest a TabView in a NavigationView. But then the navigation bar title of the tab items doesn't get displayed, just an empty navigation bar.
有什么解决办法吗?
struct ContentView: View {
var body: some View {
NavigationView {
TabView {
Text("Tab 1")
.navigationBarTitle("Tab 1") // is ignored, only an empty string is displayed
.tabItem {
Text("Tab 1")
}
Text("Tab 2")
.navigationBarTitle("Tab 2") // is ignored, only an empty string is displayed
.tabItem {
Text("Tab 2")
}
}
// this would display a navigation bar title, but then the title is the same for all tab items
//.navigationBarTitle("TabView title")
}
}
}
推荐答案
这里是可能的解决方案.使用 Xcode 11.4/iOS 13.4 测试
Here is possible solution. Tested with Xcode 11.4 / iOS 13.4
struct ContentView: View {
@State private var title = ""
var body: some View {
NavigationView {
TabView {
Text("Tab 1")
.onAppear { self.title = "Tab 1" }
.tabItem {
Text("Tab 1")
}
Text("Tab 2")
.onAppear { self.title = "Tab 2" }
.tabItem {
Text("Tab 2")
}
}
.navigationBarTitle(title)
}
}
}
这篇关于SwiftUI - 在 NavigationView 中嵌套 TabView 时不显示导航栏标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文