【问题标题】:Views content not showing on switching to other tabs using TabbedView of SwiftUI使用 SwiftUI 的 TabbedView 切换到其他选项卡时查看内容不显示
【发布时间】:2023-03-25 07:10:01
【问题描述】:

我通过引用https://developer.apple.com/documentation/swiftui/tabview 使用SwiftUI 框架实现TabbedView

在模拟器上运行时,只显示第一个选项卡视图内容,其他选项卡内容不显示。即使在重新启动 XCode、模拟器等之后。

应用视频链接:https://youtu.be/Gibu8jfQQ5I

struct ContentView : View {
    var body: some View {
         TabbedView {
            Text("The First Tab")
                .tabItem {
                    Image(systemName: "1.square.fill")
                    Text("First")
            }
            Text("Another Tab")
                .tabItem {
                    Image(systemName: "2.square.fill")
                    Text("Second")
            }
            Text("The Last Tab")
                .tabItem {
                    Image(systemName: "3.square.fill")
                    Text("Third")
            }
        }.font(.headline)
    }
}

感谢您的帮助和建议!

【问题讨论】:

  • 尝试将.padding() 添加到TabbedView
  • 嗯,我使用了相同的代码,它在模拟器和预览选项卡上运行良好。我正在使用 beta 5。您也可以尝试删除并重新添加模拟器。

标签: ios swiftui tabbed-view


【解决方案1】:

在 beta 5 中,您的代码可以正常工作,而且 TabbedView 已重命名为 TabView。如果您还不能升级到 beta 5,要修复 beta 4 中的问题,您需要在每个视图中添加 .tag(n)

struct ContentView : View {
    var body: some View {
         TabbedView {
            Text("The First Tab").tag(1)
                .tabItem {
                    Image(systemName: "1.square.fill")
                    Text("First")
            }
            Text("Another Tab").tag(2)
                .tabItem {
                    Image(systemName: "2.square.fill")
                    Text("Second")
            }
            Text("The Last Tab").tag(3)
                .tabItem {
                    Image(systemName: "3.square.fill")
                    Text("Third")
            }
        }.font(.headline)
    }
}

【讨论】:

  • 不幸的是,这不会显示“更多”选项卡下的选项卡。所以标签 5、6、7 等。对我来说看起来像是一个错误。
猜你喜欢
  • 1970-01-01
  • 2015-02-27
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
相关资源
最近更新 更多