简体   繁体   English

SwiftUI - TabView 初始 tabItem 不显示文本

[英]SwiftUI - TabView initial tabItem not displaying text

Summary of the problem问题总结

I have HomeView which contains the TabView (which is inside a NavigationView, see the code below).我有 HomeView,其中包含 TabView(位于 NavigationView 内,请参见下面的代码)。 If I am to load the HomeView from another view (LoginView) it loads as expected and everything works.如果我要从另一个视图(LoginView)加载 HomeView,它会按预期加载并且一切正常。 If I try to load the HomeView directly like this (code is in my ContentView):如果我尝试像这样直接加载 HomeView(代码在我的 ContentView 中):

if authService.isLoggedIn {
    HomeView()
} else {
    LoginView()
}

it again loads the HomeView with the tabs at the bottom but my first tab is missing text and only displays its image.它再次加载带有底部选项卡的 HomeView,但我的第一个选项卡缺少文本,仅显示其图像。 Strangely if I switch to a tab (ie click Account) the text on the first tab appears again.奇怪的是,如果我切换到一个选项卡(即单击帐户),第一个选项卡上的文本会再次出现。

Here is the code for my TabView:这是我的 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)
}

Expected Result预期结果

预期结果

Actual Result实际结果

实际结果

Note: Notice how the icon is displayed at lower level than other icons, so the text is actually not displaying at all注意:请注意图标是如何显示在比其他图标更低的级别上的,因此文本实际上根本没有显示

What I Tried So Far到目前为止我尝试了什么

I did try only a few things, because the TabView is not very well documented on Apple's official documentation.我确实只尝试了几件事,因为 Apple 的官方文档中没有很好地记录 TabView。

  • I tried moving the NavigationView both down and up the view hierarchy我尝试在视图层次结构中上下移动 NavigationView
  • Setting another tab for initial selection为初始选择设置另一个选项卡
  • Switching places of Image() and Text() inside the.tabItem section在 .tabItem 部分中切换 Image() 和 Text() 的位置
  • Searching for a similar problem through the internet通过互联网搜索类似的问题

I have found a solution, Turns out what was showing at the bottom was that navigation bar title.我找到了一个解决方案,原来底部显示的是导航栏标题。 which I was setting to an empty string.我将其设置为空字符串。 So I changed my navigation bar titles on every View of the TabView elements: The code now looks like this:因此,我更改了 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)
}

and It works as expected.它按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM