Inspect is a new approach at accessing UIKit and AppKit components from within SwiftUI more safely and swifty. It is heavily inspired by SwiftUI-Introspect
, but does not use any of the old code.
SwiftUI | UIKit | AppKit | Notes |
---|---|---|---|
TextField | UITextField | NSTextField | |
TextEditor | UITextEditor | NSTextView | |
ScrollView | UIScrollView | NSScrollView | |
List/Form | ListInspectionNativeView* | NSTableView | |
any View | ListCellInspectionNativeView* | not implemented yet | This can be called on any view that represents a List or Form Cell. The function is called inspectListOrFormCell . |
Button | is no native control | NSButton | |
Toggle | UISwitch | NSButton | |
Slider | UISlider | NSSlider | |
Stepper | UIStepper | NSStepper | |
DatePicker | UIDatePicker | NSDatePicker | |
Picker | UISegmentedControl | NSSegmentedControl | This currently only supports the PickerStyle.segmentedControl right now, which is why the method is called inspectSegmentedControl . |
ColorPicker | UIColorWell | NSColorWell | |
NavigationView / NavigationStack | UINavigationController / UISplitViewController / UINavigationBar | is no relevant native control | The default function inspect returns a UINavigationController . UISplitViewController is available via inspectSplitViewController and UINavigationBar using inspectNavigationBar . |
TabView | UITabBarController / UITabBar | is no relevant native control | The default function inspect returns a UITabBarController . UITabBar is available via inspectTabBar . |
*on iOS, depending on the environment and OS Version, List
and Form
can be both a UITableView
and a UICollectionView
and thus their subviews can be either UITableViewCell
or UICollectionViewCell
. ListInspectionNativeView
and ListCellInspectionNativeView
are enums, which contain the found view as an associated value (e.g. ListInspectionNativeView.tableView(UITableView)
). In most instances you should propably support both cases.
The biggest differences between Inspect and Introspect are:
InspectionView
), but instead goes through the levels of the view hierarchy until it has found the correct view. It does so by matching the InspectionView
frame against the frame of any potential result candidate.inspect()
on a SwiftUI TextField
and get a UITextField
. You cannot call inspect()
to retrieve a UITextField
on any other SwiftUI type, not even on a modified TextField
. This does not apply to UITableViewCell
resp. UICollectionViewCell
, which do not have a corresponding SwiftUI type.Inspect works by adding a InspectionView
, as a background view, to the view hierarchy. It then looks for a view that has the same global frame as the InspectionView
and is of the type you are looking for. It is a little bit more complicated in some cases, where the looked-for frame differs from the InspectionView
's frame, but you can find all of that out by looking at the code.
Please note that this introspection method might break in future SwiftUI releases. Future implementations might not use UIKit elements that are being looked for. Though the library is unlikely to crash, the .inspect()
method will not be called in those cases.
Inspect
can be used in production and follows some ground-rules to operate:
TextField("Placeholder", text: $textValue)
.inspect { field in
field.layer.backgroundColor = UIColor.red.cgColor
}
https://github.com/quintschaf/SwiftUI-Inspect.git
Picker
styles other than .segmentedControl
, propably with an enum return (like a List)Special thanks to Siteline, Loïs Di Qual and all other contributors of SwiftUI-Introspect
for a lot of inspiration.
link |
Stars: 2 |
Last commit: 1 week ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics