I'm trying to implement a feature to an app I'm working on so that when a user taps a tab twice, it will automatically send the user back to the tab's initial view.
Suppose that I want the following "Devices" tab button to reload the view on a double tap:
This is the code I've been trying to use to solve this issue:
Tab View {
DevicesScreen()
.tabItem {
Image(systemName: "tv")
Text("Devices")
}.onTapGesture(count: 2) {
DevicesScreen()
}
}.font(.headline)
However, the result of the onTapGesture won't switch the view, therefore I wanted to ask whether there is another solution to the problem.
Thanks in advance.
try this:
struct ContentView: View {
@State var selectedTab: Tab = .home
var body: some View {
TabView(selection: $selectedTab) {
Text("Home Screen")
.tabItem { Text("Home") }
.tag(Tab.home)
Text("More Screen")
.tabItem { Text("More") }
.tag(Tab.more)
Text("bla Screen")
.tabItem { Text("bla") }
.tag(Tab.bla)
}.onTapGesture(count: 2) {
if self.selectedTab == .more {
self.selectedTab = .home
}
}
}
}
extension ContentView {
enum Tab: Hashable {
case home
case more
case bla
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With