Code Block swiftstruct ContentView: View { |
/* |
This "tabSelection" variable is used by the TabView to |
update which tab is currently selected. This value will |
correspond to the value of the ".tag" modifer on each |
view of the TabView. |
The "@State" property wrapper is needed so that SwiftUI |
can update the value of this variable. |
*/ |
@State |
private var tabSelection = 0 |
|
var body: some View { |
TabView(selection: $tabSelection) { |
FirstTabView() |
.tabItem { |
/* |
The ".tabItem" modifier is used to set |
the contents of the button on the tab bar. |
This view will accept an optional Image and/or Text view. |
*/ |
Image(systemName: "1.square.fill") |
Text("Tab Label 1") |
} |
.tag(0) /* When this tab is selected, tabSelection will update with this value */ |
|
SecondTabView() |
.tabItem { |
Image(systemName: "2.square.fill") |
Text("Tab Label 2") |
} |
.tag(1) |
} |
} |
} |
|
struct FirstTabView: View { |
var body: some View { |
/* |
This is where you would create the content of your view inside |
each tab. Other views, functions, etc. |
*/ |
|
NavigationView { |
Text("First Tab View Body") |
.navigationTitle("First") |
} |
} |
} |
|
struct SecondTabView: View { |
var body: some View { |
NavigationView { |
Text("Second Tab View Body") |
.navigationTitle("Second") |
} |
} |
} |