GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: Unable to swipe gesture to change between tabs of TabView

Forums > SwiftUI

My simple iOS app has a TabView within the ContentView containing three tabs: ProfileFormView, InvestFormView, EarningsView.

Here is the link to the GitHub repository.

My ContentView code is as follows:

TestApp
import SwiftUI

struct ContentView: View {
    @State private var tabSelection = 1

    var body: some View {
        TabView(selection: $tabSelection) {
            ProfileFormView(tabSelection: $tabSelection)
                .tabItem {
                    Image(systemName: "square.and.pencil")
                    Text("Profile")
                }
                .tag(1)
            InvestFormView(tabSelection: $tabSelection)
                .tabItem {
                    Image(systemName: "dollarsign")
                    Text("Invest")
                }
                .tag(2)
            EarningsView()
                .tabItem {
                    Image(systemName: "chart.line.uptrend.xyaxis")
                    Text("Results")
                }
                .tag(3)
        }
//        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
    }
}

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

If I uncomment .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never)), I gain the ability to swipe left and right to navigate between tabs BUT I lose the three tab icons at the bottom of the view and the view formatting is generally disorganized.

Any ideas how to gain swipe navigation ability while keeping the tab icons at the bottom?

2      

Hi,

Take a look at this article, it does what you want, but it does not have the animation.

2      

You can make a custom one. Sorry if not perfect. Was made in Playgrounds on iPad. On it resembles native one.

struct ContentView: View {
    @State private var tabSelection = 1

    var body: some View {
        ZStack(alignment: .bottom) {
            TabView(selection: $tabSelection) {
                Text("View One")
                    .tag(1)
                Text("View Two")
                    .tag(2)
                Text("View Three")
                    .tag(3)
            }
            .tabViewStyle(.page(indexDisplayMode: .never))

            TabButtons(selectedTab: $tabSelection)
        }
    }
}

struct TabButtons: View {
    @Binding var selectedTab: Int

    var body: some View {
        HStack {
            Spacer()
            VStack(spacing: 5) {
                Image(systemName: "square.and.pencil")
                Text("Profile")
                    .font(.caption)
            }
            .onTapGesture {
                selectedTab = 1
            }
            .foregroundColor(selectedTab == 1 ? .blue : .gray)

            Spacer()
            VStack(spacing: 5) {
                Image(systemName: "dollarsign")
                Text("Invest")
                    .font(.caption)
            }
            .onTapGesture {
                selectedTab = 2
            }
            .foregroundColor(selectedTab == 2 ? .blue : .gray)

            Spacer()
            VStack(spacing: 5) {
                Image(systemName: "chart.line.uptrend.xyaxis")
                Text("Results")
                    .font(.caption)
            }
            .onTapGesture {
                selectedTab = 3
            }
            .foregroundColor(selectedTab == 3 ? .blue : .gray)
            Spacer()
        }
        .padding(.vertical)
        .background(
            Rectangle()
                .fill(.quaternary)
        )
    }
}

2      

I was going to suggest that you look at Flashzilla to do with .gesture. But either way you can have the PageView OR TabView (then have a gesture swipe)

3      

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket 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.