/

SwiftUI: How to Create a Tab View

SwiftUI: How to Create a Tab View

Tab views are a common UI pattern in iOS apps, allowing users to navigate between different sections of an app by tapping icons or labels at the bottom of the screen. In SwiftUI, creating a tab view is made simple with the TabView view.

Here is the basic implementation of a TabView:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import SwiftUI

struct ContentView: View {

var body: some View {
TabView {
Text("First")
.tabItem {
Label("First", systemImage: "tray")
}

Text("Second")
.tabItem {
Label("Second", systemImage: "calendar")
}
}
}
}

The TabView contains two child views, represented by Text views in this example. The tabItem modifier is used to assign a label to each tab using the Label view.

Tab View Example

As you can see, the TabView creates a tab bar at the bottom of the screen with the provided labels. You can replace the Text views with your own custom views to suit your app’s needs.

Tags: SwiftUI, Tab View, iOS development