SwiftUI: how to create a Tab View

It’s common in iOS apps to use a Tab View. The one with a few choices at the bottom, and you can completely switch what’s in the screen by tapping the icon / label.

SwiftUI conveniently provides us a view called TabView, which mak…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

It’s common in iOS apps to use a Tab View. The one with a few choices at the bottom, and you can completely switch what’s in the screen by tapping the icon / label.

SwiftUI conveniently provides us a view called TabView, which makes it easy to implement such a UI pattern.

Here’s the simplest possible example of a TabView:

import SwiftUI

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

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

And here’s the result:

See? We have a TabView view, and inside it, we have 2 views.

Both are Text views to make it simple.

Their tabItem modifier will add them to the TabView with a label provided as a Label view.

Of course you will want to use a custom view instead of Text in most cases.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-10-04T05:00:00+00:00) SwiftUI: how to create a Tab View. Retrieved from https://www.scien.cx/2021/10/04/swiftui-how-to-create-a-tab-view/

MLA
" » SwiftUI: how to create a Tab View." flaviocopes.com | Sciencx - Monday October 4, 2021, https://www.scien.cx/2021/10/04/swiftui-how-to-create-a-tab-view/
HARVARD
flaviocopes.com | Sciencx Monday October 4, 2021 » SwiftUI: how to create a Tab View., viewed ,<https://www.scien.cx/2021/10/04/swiftui-how-to-create-a-tab-view/>
VANCOUVER
flaviocopes.com | Sciencx - » SwiftUI: how to create a Tab View. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/04/swiftui-how-to-create-a-tab-view/
CHICAGO
" » SwiftUI: how to create a Tab View." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/10/04/swiftui-how-to-create-a-tab-view/
IEEE
" » SwiftUI: how to create a Tab View." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/10/04/swiftui-how-to-create-a-tab-view/. [Accessed: ]
rf:citation
» SwiftUI: how to create a Tab View | flaviocopes.com | Sciencx | https://www.scien.cx/2021/10/04/swiftui-how-to-create-a-tab-view/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.