Implement own selector

This commit is contained in:
Lois Di Qual 2019-11-27 11:15:04 -08:00
parent 1f6021e50b
commit 620de5720e
1 changed files with 33 additions and 2 deletions

View File

@ -50,7 +50,7 @@ Slider | UISlider | `.introspectSlider()` | Slider
Stepper | UIStepper | `.introspectStepper()` | Stepper
DatePicker | UIDatePicker | `.introspectDatePicker()` | DatePicker
**Missing an element?** Please [create an issue](https://github.com/timbersoftware/SwiftUI-Introspect/issues).
**Missing an element?** Please [create an issue](https://github.com/timbersoftware/SwiftUI-Introspect/issues). As a temporary solution, you can [implement your own selector](#implement-your-own-selector).
### Cannot implement
@ -105,4 +105,35 @@ TextField("Text Field", text: $textFieldValue)
.introspectTextField { textField in
textField.layer.backgroundColor = UIColor.red.cgColor
}
```
```
Implement your own selector
---------------------------
**Missing an element?** Please [create an issue](https://github.com/timbersoftware/SwiftUI-Introspect/issues).
In case Introspect doesn't support the SwiftUI element that you're looking for, you can implement your own selector. For example, to look for a `UITextField`:
```swift
extension View {
public func introspectTextField(customize: @escaping (UITextField) -> ()) -> some View {
return self.background(IntrospectionView(
selector: { introspectionView in
guard let viewHost = Introspect.findViewHost(from: introspectionView) else {
return nil
}
return Introspect.firstSibling(containing: UITextField.self, from: viewHost)
},
customize: customize
))
}
}
```
You can use any of the following [methods](https://github.com/timbersoftware/SwiftUI-Introspect/blob/master/Introspect/Introspect.swift#L3-L71) to inspect the hierarchy:
- `Introspect.findChild(ofType:in:)`
- `Introspect.firstSibling(containing:from:)`
- `Introspect.findAncestor(ofType:from:)`
- `Introspect.findHostingView(from:)`
- `Introspect.findViewHost(from:)`