Code
SwiftUI Introspect – Introspect underlying UIKit components from SwiftUI

Introspect allows you to get the underlying UIKit or AppKit element of a SwiftUI view.
For instance, with Introspect you can access UITableView
to modify separators, or UINavigationController
to customize the tab bar.
How SwiftUI Introspect works
Introspect works by adding a custom IntrospectionView
to the view hierarchy, then looking into the UIKit hierarchy to find the relevant view.
For instance, when introspecting a TextField
, it will:
- Add
IntrospectionView
as an overlay ofTextField
- Get the view host of the introspection view (which is alongside the view host of the
UITextField
) - Get the previous sibling containing
UITextField
Please note that this introspection method might break in future SwiftUI releases. Future implementations might not use the same hierarchy, or might not use UIKit elements that are being looked for. Though the library is unlikely to crash, the .introspect()
method will not be called in those cases.
Usage in production
Introspect
is meant to be used in production. It does not use any private API. It only inspects the view hierarchy using publicly available methods. The library takes a defensive approach to inspecting the view hierarchy: there is no hard assumption that elements are laid out a certain way, there is no force-cast to UIKit classes, and the introspect()
methods are simply ignored if UIKit views cannot be found.
