[英]SwiftUI - TabView initial tabItem not displaying text
我有 HomeView,其中包含 TabView(位于 NavigationView 内,请参见下面的代码)。 如果我要从另一个视图(LoginView)加载 HomeView,它会按预期加载并且一切正常。 如果我尝试像这样直接加载 HomeView(代码在我的 ContentView 中):
if authService.isLoggedIn {
HomeView()
} else {
LoginView()
}
它再次加载带有底部选项卡的 HomeView,但我的第一个选项卡缺少文本,仅显示其图像。 奇怪的是,如果我切换到一个选项卡(即单击帐户),第一个选项卡上的文本会再次出现。
这是我的 TabView 的代码:
NavigationView {
TabView(selection: $selected) {
PlayView()
.tabItem {
Image(systemName: "play.circle.fill")
.font(.system(size: 24))
Text("Play")
}
.navigationBarHidden(true)
.navigationBarTitle("")
.tag(1)
AccountView()
.tabItem {
Image(systemName: "person.circle.fill")
.font(.system(size: 24))
Text("Account")
}
.tag(3)
NotificationsView()
.tabItem {
Image(systemName: "bell.fill")
.font(.system(size: 24))
Text("Notifications")
}
.tag(4)
}
.accentColor(Color(K.Colors.Secondary))
.navigationBarTitle("")
.navigationBarHidden(true)
}
注意:请注意图标是如何显示在比其他图标更低的级别上的,因此文本实际上根本没有显示
我确实只尝试了几件事,因为 Apple 的官方文档中没有很好地记录 TabView。
我找到了一个解决方案,原来底部显示的是导航栏标题。 我将其设置为空字符串。 因此,我更改了 TabView 元素的每个视图上的导航栏标题:代码现在如下所示:
NavigationView {
TabView(selection: $selected) {
PlayView()
.tabItem {
Image(systemName: "play.circle.fill")
.font(.system(size: 24))
Text("Play")
}
.navigationBarHidden(true)
.navigationBarTitle("Play")
.tag(1)
AccountView()
.tabItem {
Image(systemName: "person.circle.fill")
.font(.system(size: 24))
Text("Account")
}
.navigationBarHidden(true)
.navigationBarTitle("Account")
.tag(3)
NotificationsView()
.tabItem {
Image(systemName: "bell.fill")
.font(.system(size: 24))
Text("Notifications")
}
.navigationBarHidden(true)
.navigationBarTitle("Notifications")
.tag(4)
}
.accentColor(Color(K.Colors.Secondary))
.navigationBarTitle("")
.navigationBarHidden(true)
}
它按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.