Episode #531

SwiftUI Escape Hatch

17 minutes
Published on June 2, 2022
Sometimes we run into issues where SwiftUI doesn't quite do what we need. In some cases, SwiftUI views are powered by UIKit under the hood. Wouldn't it be great (and devious) to dig into the underlying UIKit views to customize things when vanilla SwiftUI just won't cut it? In this episode we'll look at a technique for discovering the UIKit underneath SwiftUI views.

This episode was heavily inspired by this gist by Ian Keen on "discovering" UIKit Views. It also extends this idea to view controllers, which uses the responder chain to search for a parent view controller of a given type. Thanks Ian!

A quick disclaimer first: Now it’s important to note that this is a brittle approach. Future versions of SwiftUI may completely change how certain views work, and they may stop relying on UIkit. That said, you could provide this as a workaround for current versions of SwiftUI and adapt to the newer ways once they come out.

Links

  • Introspect - If you're looking for a more robust implementation of this idea (or something that has more users than just you), checkout this project on GitHub. It employs the same idea. It also extends the idea to AppKit views as well.
  • Reveal - This is the app I use to show view hierarchies. It is essential to my work and way more useful than the builtin view debugger in Xcode.

This episode uses Swift 5.5, Xcode 13.4.

Want more? Subscribers can view all 585 episodes. New episodes are released regularly.

Subscribe to get access →

Source Code

View on GitHub Download Source