> For the complete documentation index, see [llms.txt](https://ivansaul.gitbook.io/swiftui-cookbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ivansaul.gitbook.io/swiftui-cookbook/swiftui/readme/create-a-tab-bar-with-tabview-in-swiftui.md).

# Create a Tab Bar with TabView in SwiftUI

In SwiftUI, `TabView` is a powerful and flexible component that allows you to easily create a tab bar for navigating between multiple views. In this tutorial, we will explore how to use `TabView` to create a modern and fluid user experience.

## Basic `TabView` Example

To start, let's look at a basic example of how to create a `TabView`.

```swift
TabView {
    Text("Tab 1")
    Text("Tab 2")
}
```

In this example, we are simply adding two text views as tabs. Each view will become a tab within the `TabView`.

## Adding Tabs to the Tab Bar

To add visual elements and labels to the tabs, we use the `tabItem` modifier.

```swift
TabView {
    Text("Home Tab")
        .tabItem {
            Image(systemName: "house")
            Text("Home")
        }
    Text("Settings Tab")
        .tabItem {
            Image(systemName: "gear")
            Text("Settings")
        }
}
```

In this case, each tab includes an icon and text that will appear in the tab bar. This makes the tabs more intuitive and easier to navigate.

<figure><img src="https://1046726652-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7YPi723jowFBYIdu4XSB%2Fuploads%2FnjpfxzqZTgDdlZvVXi2S%2FScreenshot%202024-06-25%20at%209.05.17%20PM.png?alt=media&amp;token=b265eb6b-233c-45a2-8730-92839ee32d73" alt=""><figcaption><p>Adding Tabs to Tab Bar</p></figcaption></figure>

## Changing the Tab Bar Appearance

We can further customize the appearance of the tab bar using the `tint` modifier.

```swift
TabView {
    Text("Home Tab")
        .tabItem {
            Image(systemName: "house")
            Text("Home")
        }
    Text("Settings Tab")
        .tabItem {
            Image(systemName: "gear")
            Text("Settings")
        }
}
.tint(.orange)
```

The `.tint(.orange)` modifier changes the color of the currently selected tab item. To modify the background color of the tab bar, we need to use `UITabBar.appearance()`.

```swift
struct TabViewSwiftUI: View {
    init() {
        UITabBar.appearance().backgroundColor = UIColor.lightGray
    }

    var body: some View {
        TabView {
            Text("Home Tab")
                .tabItem {
                    Image(systemName: "house")
                    Text("Home")
                }
            Text("Settings Tab")
                .tabItem {
                    Image(systemName: "gear")
                    Text("Settings")
                }
        }
    }
}
```

In the example above, we change the background color of the tab bar to blue using `UITabBar.appearance().backgroundColor`.

<figure><img src="https://1046726652-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7YPi723jowFBYIdu4XSB%2Fuploads%2F7PJrP8Vj8A0qsUTkKn91%2FScreenshot%202024-06-25%20at%209.22.16%20PM.png?alt=media&amp;token=fd62b957-2d74-40c3-affb-07cf43f640bd" alt=""><figcaption><p>Change Tab Bar Appearance</p></figcaption></figure>

### Navigating Between Tabs Programmatically

To change the selected tab, we need to set which one is displayed initially, we use the `tag` modifier and the `selection` binding.

```swift
struct TabViewSwiftUI: View {
    @State var selectedTab: Int = 1
    var body: some View {
        TabView(selection: $selectedTab) {
            Text("Home Tab")
                .tabItem {
                    Image(systemName: "house")
                    Text("Home")
                }
                .tag(0)

            VStack {
                Text("Messages Tab")

                Button("Go Home") {
                    selectedTab = 0
                }
                .buttonStyle(.bordered)
            }
            .tabItem {
                Image(systemName: "message.badge")
                Text("Messages")
            }
            .tag(1)
            
            Text("Settings Tab")
                .tabItem {
                    Image(systemName: "gear")
                    Text("Settings")
                }
                .tag(2)
        }
    }
}
```

In this example, the tab with `tag(1)` will be displayed initially thanks to the `selectedTab` state binding. Additionally, we are using a `Button` to change the tab when the user taps on it.

<figure><img src="https://1046726652-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7YPi723jowFBYIdu4XSB%2Fuploads%2FryOod4ODNVNOjilmf5Yn%2FScreenshot%202024-06-25%20at%2010.02.35%20PM.png?alt=media&amp;token=407b75fb-f449-49dd-b952-3ea75fa0954f" alt=""><figcaption><p>Navigating Between Tabs</p></figcaption></figure>

## Changing the Tab Bar Style

We can use the `tabViewStyle` modifier to apply a different style to the tab bar.

```swift
struct TabViewSwiftUI: View {
    var body: some View {
        TabView {
            Color.red
                .ignoresSafeArea()
            Color.green
                .ignoresSafeArea()
            Color.blue
                .ignoresSafeArea()
        }
        .ignoresSafeArea()
        .tabViewStyle(PageTabViewStyle())
    }
}
```

In this example, we are using `PageTabViewStyle()` to create a tab bar that behaves like a page that can be swiped.

<figure><img src="https://1046726652-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7YPi723jowFBYIdu4XSB%2Fuploads%2FWjeTW6jBpSnM62WxbTov%2FScreenshot%202024-06-26%20at%201.06.05%20AM.png?alt=media&amp;token=8d80ef9e-7e01-4917-b13e-962ff12b74c6" alt=""><figcaption><p>Changing TabViewStyle</p></figcaption></figure>
