UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Tabbar onTapGesture breaking navigation

Forums > SwiftUI

If I add

.onTapGesture { }

to my Tab bar items it breaks the navigation of the content views

Reference: https://www.hackingwithswift.com/books/ios-swiftui/creating-tabs-with-tabview-and-tabitem

It breaks the navigation within the Detail page, tapping Back does nothing.

What am im doing wrong?

Full example below

import SwiftUI

struct ContentView: View {

    @State private var selectedTab = Tab.First

    private enum Tab {
        case First, Second
    }

    var body: some View {
        TabView(selection: $selectedTab){
            FirstView()
                .onTapGesture {
                    self.selectedTab = Tab.First
                }
            .tabItem {
                Image(systemName: "1.circle")
                Text("Drops")
            }.tag(Tab.First)
            SecondView()
                .onTapGesture {
                    self.selectedTab = Tab.Second
            }
            .tabItem {
                Image(systemName: "1.circle")
                Text("secss")
            }.tag(Tab.Second)
        }
    }
}

struct FirstView: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DetailView()) {
                    Text("Show Detail View")
                }.navigationBarTitle("Navigation")
            }
        }
    }
}

struct SecondView: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DetailView()) {
                    Text("Secomd Detail View")
                }.navigationBarTitle("Navigation")
            }
        }
    }
}

struct DetailView: View {
    var body: some View {
        Text("This is the detail view")
    }
}

2      

You do not need the

.onTapGesture {
    self.selectedTab = Tab.First
}

then it should work fine going to detail view and back.

If you going to the first view then you do not need (selection: $selectedTab) because will default to it, however if @State private var selectedTab = Tab.Second then leave it in.

PS i found that you need to put .font(.system(size: 20)) modifier on the SF Image because when you go to the accessility font of large the symbol go off the Tab bar

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.