How to set a default Tab in SwiftUIs TabView?
Define a State
with the default value and bind it to TabView
:
@State var selection = 1
,,,
TabView(selection: $selection)
,,,
Then assign a tag
to each tabItem
:
,,,
.tabItem {
Image(systemName: "bolt.horizontal.fill")
Text("Trend")
}.tag(1)
,,,
Now you can use them together, selection
=== tag
@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)
}
In this case I set default selected the third Tab
. Change selection
to the desired Tab
.