我在 SwiftUI 中有一个 TabView
,并希望在启动应用程序时将第二个选项卡设为默认选项卡。
我还没有找到任何文档来提供这种行为,但这应该是可能的。大多数应用程序都将中间选项卡作为默认选项卡。
TabView {
QuestionView()
.tabItem {
Image(systemName: "questionmark")
Text("Questions")
}
DataView()
.tabItem {
Image(systemName: "chart.bar.fill")
Text("Data")
}
Text("Empty Tab right now")
.tabItem {
Image(systemName: "bolt.horizontal.fill")
Text("Trend")
}
}
最佳答案
@State private var selection = 3
TabView(selection:$selection) {
QuestionView()
.tabItem {
Image(systemName: "questionmark")
Text("Questions")
}
.tag(1)
DataView()
.tabItem {
Image(systemName: "chart.bar.fill")
Text("Data")
}
.tag(2)
Text("Empty Tab right now")
.tabItem {
Image(systemName: "bolt.horizontal.fill")
Text("Trend")
}
.tag(3)
}
在本例中,我设置默认选择第三个选项卡
。将选择
更改为所需的选项卡
。
关于swift - 如何在 SwiftUI TabView 中设置默认选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57802440/