Root introspection

This commit is contained in:
Lois Di Qual 2019-11-27 09:52:38 -08:00
parent 5034d7dab8
commit 57479571f3
3 changed files with 64 additions and 23 deletions

View File

@ -104,7 +104,6 @@ public struct IntrospectionView<ViewType: UIView>: UIViewRepresentable {
public func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<IntrospectionView>) {
DispatchQueue.main.asyncAfter(deadline: .now()) {
guard let targetView = self.selector(uiView) else {
print("Couldn't find a view of type \(ViewType.self). Please make sure you apply introspect*() on a subview of the element to introspect.")
return
}
self.customize(targetView)
@ -140,7 +139,6 @@ public struct IntrospectionViewController<ViewControllerType: UIViewController>:
return
}
guard let targetView = self.selector(hostingViewController) else {
print("Couldn't find a view controller of type \(ViewControllerType.self). Please make sure you apply introspect*() on a subview of the element to introspect.")
return
}
self.customize(targetView)
@ -154,7 +152,18 @@ extension View {
public func introspectTableView(customize: @escaping (UITableView) -> ()) -> some View {
return background(IntrospectionView(
selector: { introspectionView in
Introspect.findAncestor(ofType: UITableView.self, from: introspectionView)
// Search in ancestors
if let tableView = Introspect.findAncestor(ofType: UITableView.self, from: introspectionView) {
return tableView
}
guard let viewHost = Introspect.findViewHost(from: introspectionView) else {
return nil
}
// Search in siblings
return Introspect.firstSibling(containing: UITableView.self, from: viewHost)
},
customize: customize
))
@ -163,7 +172,18 @@ extension View {
public func introspectScrollView(customize: @escaping (UIScrollView) -> ()) -> some View {
return background(IntrospectionView(
selector: { introspectionView in
Introspect.findAncestor(ofType: UIScrollView.self, from: introspectionView)
// Search in ancestors
if let tableView = Introspect.findAncestor(ofType: UIScrollView.self, from: introspectionView) {
return tableView
}
guard let viewHost = Introspect.findViewHost(from: introspectionView) else {
return nil
}
// Search in siblings
return Introspect.firstSibling(containing: UIScrollView.self, from: viewHost)
},
customize: customize
))

View File

@ -29,7 +29,7 @@ struct ListExample: View {
HStack {
VStack {
Text("Before")
Text("Default")
List {
Text("Item 1")
Text("Item 2")
@ -37,15 +37,25 @@ struct ListExample: View {
}
VStack {
Text("After")
Text("List.introspectTableView()")
List {
Text("Item 1")
Text("Item 2")
.introspectTableView { tableView in
tableView.separatorStyle = .none
}
}
.introspectTableView { tableView in
tableView.separatorStyle = .none
}
}
VStack {
Text("child.introspectTableView()")
List {
Text("Item 1")
Text("Item 2")
.introspectTableView { tableView in
tableView.separatorStyle = .none
}
}
}
}
@ -68,11 +78,22 @@ struct NavigationExample: View {
struct ScrollViewExample: View {
var body: some View {
ScrollView {
Text("Customized")
HStack {
ScrollView {
Text("Default")
}
ScrollView {
Text("ScrollView.introspectScrollView()")
}
.introspectScrollView { scrollView in
scrollView.layer.backgroundColor = UIColor.red.cgColor
}
ScrollView {
Text("child.introspectScrollView()")
.introspectScrollView { scrollView in
scrollView.layer.backgroundColor = UIColor.green.cgColor
}
}
}
}
}

View File

@ -5,17 +5,17 @@ Introspect allows you to get the underlying UIKit element of a SwiftUI view.
### Implemented
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
TabbedView | UITabBarController | `.introspectTabBarController()` on a tab bar child
TextField | UITextField | `.introspectTextField()`
Toggle | UISwitch | `.introspectSwitch()`
Slider | UISlider | `.introspectSlider()`
Stepper | UIStepper | `.introspectStepper()`
DatePicker | UIDatePicker | `.introspectDatePicker()`
SwiftUI | UIKit | Introspect | Target
--- | --- | --- | ---
List | UITableView | `.introspectTableView()` | List, or List child
ScrollView | UIScrollView | `.introspectScrollView()` | ScrollView, or ScrollView child
NavigationView | UINavigationController | `.introspectNavigationController()` | NavigationView child
TabbedView | UITabBarController | `.introspectTabBarController()` | TabbedView
TextField | UITextField | `.introspectTextField()` | TextField
Toggle | UISwitch | `.introspectSwitch()` | Toggle
Slider | UISlider | `.introspectSlider()` | Slider
Stepper | UIStepper | `.introspectStepper()` | Stepper
DatePicker | UIDatePicker | `.introspectDatePicker()` | DatePicker
### Cannot implement