使用SwiftUI的TabbedView查看切换到其他选项卡时未显示的内容

时间:2019-08-05 09:51:39

标签: ios swiftui tabbed-view

我正在通过引用https://developer.apple.com/documentation/swiftui/tabview使用TabbedView框架来实现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)
    }
}

感谢您的帮助和建议!

1 个答案:

答案 0 :(得分: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)
    }
}
相关问题