ScrollView

This commit is contained in:
Lois Di Qual 2019-11-27 09:37:56 -08:00
parent 0efcaab039
commit ea9a33635d
3 changed files with 50 additions and 4 deletions

View File

@ -150,6 +150,7 @@ public struct IntrospectionViewController<ViewControllerType: UIViewController>:
extension View {
public func introspectTableView(customize: @escaping (UITableView) -> ()) -> some View {
return background(IntrospectionView(
selector: { introspectionView in
@ -159,6 +160,15 @@ extension View {
))
}
public func introspectScrollView(customize: @escaping (UIScrollView) -> ()) -> some View {
return background(IntrospectionView(
selector: { introspectionView in
Introspect.findAncestor(ofType: UIScrollView.self, from: introspectionView)
},
customize: customize
))
}
public func introspectNavigationController(customize: @escaping (UINavigationController) -> ()) -> some View {
return background(IntrospectionViewController(
selector: { $0.navigationController },
@ -213,4 +223,16 @@ extension View {
customize: customize
))
}
public func introspectDatePicker(customize: @escaping (UIDatePicker) -> ()) -> some View {
return self.background(IntrospectionView(
selector: { introspectionView in
guard let viewHost = Introspect.findViewHost(from: introspectionView) else {
return nil
}
return Introspect.firstSibling(containing: UIDatePicker.self, from: viewHost)
},
customize: customize
))
}
}

View File

@ -8,12 +8,15 @@ struct ContentView: View {
ListExample()
.tabItem { Text("List") }
.tag(0)
ScrollViewExample()
.tabItem { Text("ScrollView") }
.tag(1)
NavigationExample()
.tabItem { Text("Navigation") }
.tag(1)
.tag(2)
SimpleElementsExample()
.tabItem { Text("Simple elements") }
.tag(2)
.tag(3)
}
}
}
@ -60,11 +63,23 @@ struct NavigationExample: View {
}
}
struct ScrollViewExample: View {
var body: some View {
ScrollView {
Text("Customized")
.introspectScrollView { scrollView in
scrollView.layer.backgroundColor = UIColor.red.cgColor
}
}
}
}
struct SimpleElementsExample: View {
@State private var textFieldValue = ""
@State private var toggleValue = false
@State private var sliderValue = 0.0
@State private var datePickerValue = Date()
var body: some View {
VStack {
@ -119,6 +134,15 @@ struct SimpleElementsExample: View {
stepper.layer.backgroundColor = UIColor.green.cgColor
}
}
HStack {
DatePicker(selection: $datePickerValue) {
Text("DatePicker Red")
}
.introspectDatePicker { datePicker in
datePicker.layer.backgroundColor = UIColor.red.cgColor
}
}
}
}

View File

@ -8,20 +8,20 @@ Introspect allows you to get the underlying UIKit element of a SwiftUI view.
SwiftUI | UIKit | Introspect
--- | --- | ---
List | UITableView | `.introspectTableView()` on a list child
ScrollView | UIScrollView | `.introspectScrollView()` on a scroll view child
NavigationView | UINavigationController | `.introspectNavigationController()` on a navigation child
TextField | UITextField | `.introspectTextField()`
Toggle | UISwitch | `.introspectSwitch()`
Slider | UISlider | `.introspectSlider()`
Stepper | UIStepper | `.introspectStepper()`
DatePicker | UIDatePicker | `.introspectDatePicker()`
### Not implemented
SwiftUI | UIKit
--- | ---
ScrollView | UIScrollView
TabbedView | UITabBarController
DatePicker | UIDatePicker
### Cannot implement