I’m trying to automatically detect the currently visible screen in a SwiftUI app for debugging, logging, or development tools. Ideally, I want a solution where a container view can figure out which view is being displayed without manually tagging every single view.
Here are the approaches I’ve tried so far, which are not ideal for large apps:
Passing Self or the view instance to a container: doesn’t work because SwiftUI views are value types and selfdoesn’t exist at render time.
Using type reflection like String(describing: Content.self) in the container’s init: only gives the type, not the actually visible screen.
Adding a custom modifier like .screenName(“HomeView”) to every view: tedious and impractical for hundreds of views.
Using a base view or screen container: I wrap all screens inside a single base container, but it only shows the container’s name, not the actual screen, unless I add extra code to each view.
Tracking through a router or navigation stack: only works if the navigation system exposes the destination type, and it often returns generic types like Internal or Root instead of the actual visible screen.
All my screens live inside this base container, so I want a solution that works automatically for all of them without additional boilerplate per view.
I’m curious if anyone has a better, scalable approach to automatically detect or track the currently visible SwiftUI view. Any ideas, patterns, or best practices would be highly appreciated.