Using gestures with TabView
In the preceding recipe, we learned how to switch between tabs using the navigation pane at the bottom of the screen. However, when building an app, the client may want something else to trigger the navigation. Navigation between TabView
tabs can be performed programmatically. In this recipe, we will use a tap gesture to trigger the transition from one tab to another, but the concept can be generalized to any other programmatic trigger.
Getting ready
Create a SwiftUI app for iOS and name it TabViewWithGestures
.
How to do it
We will implement a screen with two TabView
structs where the user would be able to transition between tabs by using the tab bar at the bottom of the screen or by clicking on some text. The steps are as follows:
- Click on the
ContentView.swift
file to open it in the editor window. - Add an
@State
variable that holds the value of the currently selected tab. The variable should be located just above thebody
variable...