2

我通过参考https://developer.apple.com/documentation/swiftui/tabviewTabbedView使用SwiftUI框架来实现

在模拟器上运行时,只有第一个选项卡视图内容显示,其他选项卡内容不显示。即使在重新启动 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)
    }
}

感谢您的帮助和建议!

4

1 回答 1

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)
    }
}
于 2019-08-05T15:39:02.720 回答