introspectSegmentedControl

This commit is contained in:
Lois Di Qual 2019-12-02 17:57:55 -08:00
parent 9d9fa63e59
commit 32ee7c178d
5 changed files with 55 additions and 1 deletions

View File

@ -3,6 +3,8 @@ Changelog
## master
- Added `.introspectSegmentedControl()`.
## [0.0.5]
- Allow `.introspectNavigationController()` on NavigationView directly.

View File

@ -441,4 +441,17 @@ extension View {
customize: customize
))
}
/// Finds a `UIDatePicker` from a `SwiftUI.DatePicker`
public func introspectSegmentedControl(customize: @escaping (UISegmentedControl) -> ()) -> some View {
return inject(IntrospectionView(
selector: { introspectionView in
guard let viewHost = Introspect.findViewHost(from: introspectionView) else {
return nil
}
return Introspect.previousSibling(containing: UISegmentedControl.self, from: viewHost)
},
customize: customize
))
}
}

View File

@ -104,6 +104,7 @@ struct SimpleElementsExample: View {
@State private var toggleValue = false
@State private var sliderValue = 0.0
@State private var datePickerValue = Date()
@State private var segmentedControlValue = 0
var body: some View {
VStack {
@ -167,6 +168,18 @@ struct SimpleElementsExample: View {
datePicker.layer.backgroundColor = UIColor.red.cgColor
}
}
HStack {
Picker(selection: $segmentedControlValue, label: Text("Segmented control")) {
Text("Option 1").tag(0)
Text("Option 2").tag(1)
Text("Option 3").tag(2)
}
.pickerStyle(SegmentedPickerStyle())
.introspectSegmentedControl { segmentedControl in
segmentedControl.layer.backgroundColor = UIColor.red.cgColor
}
}
}
}

View File

@ -159,6 +159,22 @@ private struct DatePickerTestView: View {
}
}
private struct SegmentedControlTestView: View {
@State private var pickerValue = 0
let spy: () -> Void
var body: some View {
Picker(selection: $pickerValue, label: Text("Segmented control")) {
Text("Option 1").tag(0)
Text("Option 2").tag(1)
Text("Option 3").tag(2)
}
.pickerStyle(SegmentedPickerStyle())
.introspectSegmentedControl { segmentedControl in
self.spy()
}
}
}
class IntrospectTests: XCTestCase {
func testNavigation() {
@ -273,4 +289,14 @@ class IntrospectTests: XCTestCase {
TestUtils.present(view: view)
wait(for: [expectation], timeout: 1)
}
func testSegmentedControl() {
let expectation = XCTestExpectation()
let view = SegmentedControlTestView(spy: {
expectation.fulfill()
})
TestUtils.present(view: view)
wait(for: [expectation], timeout: 1)
}
}

View File

@ -58,6 +58,7 @@ Toggle | UISwitch | `.introspectSwitch()` | Toggle
Slider | UISlider | `.introspectSlider()` | Slider
Stepper | UIStepper | `.introspectStepper()` | Stepper
DatePicker | UIDatePicker | `.introspectDatePicker()` | DatePicker
Picker (SegmentedPickerStyle) | UISegmentedControl | `.introspectSegmentedControl()` | Picker
**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).
@ -67,7 +68,6 @@ SwiftUI | Why
--- | ---
Text | Not a UILabel
Image | Not a UIImageView
SegmentedControl | Not a UISegmentedControl
Button | Not a UIButton
Examples