NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to check which tab the user is on?

Forums > SwiftUI

Hi all, So i have three tabs A, B and C and inside C theres is a menu. My goal is to close that menu whenever i change tabs. How can i do this? Another way of asking is, how can i check to see what tab the user is on?

  • Nick G


I think @Environment presentationMode

then u can write states for each view and by switching the views get which one is active from environment


Thanks for the answer, im not very experienced would it be possible for you to explain to me what the code would look like?


I tried doing something like this

 DiscoverView().onTapGesture {
                    Image(systemName: "globe")

But it doesnt print "hello" when the tab is clicked


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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.