Lupyana Mbembati
Lupyana Mbembati

Reputation: 1836

TabView in SwiftUI not responding

I am trying to make an app using the TabView. The app renders and runs nicely, except for the fact that tapping on the tabs does nothing.

Here is my code, am I missing something?

TabView {
    HomeView()
        .tabItem {
            VStack {
                Image(systemName: "1.circle")
                Text("Home")
            }
        }.tag(1)
    SecondView()
        .tabItem {
            VStack {
                Image(systemName: "2.circle")
                Text("SecondView")
            }
        }.tag(2)
}

Upvotes: -1

Views: 1767

Answers (3)

Andy Couto
Andy Couto

Reputation: 1

The example from krjw worked for me, but i wanted to show an example with the best practices from Apple (e.g. using a Label instead of an Image & Text in a Vstack) and showing the .badge modifier.

I know when i started out I liked to have the entire code to copy and paste so i included everything in my file.

import SwiftUI

struct ReceivedView: View {
    var body: some View {
        Text("Received")
    }
}

struct SentView: View {
    var body: some View {
        Text("Sent")
    }
}

struct AccountView: View {
    var body: some View {
        Text("Account")
    }
}

struct ContentView: View {

    var body: some View {
        TabView {
            ReceivedView()
                .badge(2)
                .tabItem {
                    Label("Received", systemImage: "tray.and.arrow.down.fill")
                }
            SentView()
                .tabItem {
                    Label("Sent", systemImage: "tray.and.arrow.up.fill")
                }
            AccountView()
                .badge("!")
                .tabItem {
                    Label("Account", systemImage: "person.crop.circle.fill")
                }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 0

sally2000
sally2000

Reputation: 828

I had the same issue, in the end it turned out I had the accessibility option "Full keyboard access" turned on. Switching this off fixed it.

Upvotes: 4

krjw
krjw

Reputation: 4450

Here is a minimal example which works fine for me:

struct HomeView: View {
    var body: some View {
        Text("Home")
    }
}

struct SecondView: View {
    var body: some View {
        Text("SecondView")
    }
}

struct ContentView: View {

    var body: some View {
        TabView {
            HomeView()
                .tabItem {
                    VStack {
                        Image(systemName: "1.circle")
                        Text("Home")
                    }
            }.tag(1)
            SecondView()
                .tabItem {
                    VStack {
                        Image(systemName: "2.circle")
                        Text("SecondView")
                    }
            }.tag(2)
        }
    }
}

I hope this helps!

Upvotes: 2

Related Questions