Skip to content

Commit cac3385

Browse files
Add support for UICollectionView introspection (#169)
Co-authored-by: David Roman <[email protected]>
1 parent 46a563f commit cac3385

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

Introspect/ViewExtensions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ extension View {
9797
introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
9898
}
9999

100+
/// Finds a `UICollectionView` from a `SwiftUI.List`, or `SwiftUI.List` child.
101+
public func introspectCollectionView(customize: @escaping (UICollectionView) -> ()) -> some View {
102+
introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
103+
}
104+
105+
/// Finds a `UICollectionView` from a `SwiftUI.List`, or `SwiftUI.List` child. You can attach this directly to the element inside the list.
106+
public func introspectCollectionViewCell(customize: @escaping (UICollectionViewCell) -> ()) -> some View {
107+
introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
108+
}
109+
100110
/// Finds a `UIScrollView` from a `SwiftUI.ScrollView`, or `SwiftUI.ScrollView` child.
101111
public func introspectScrollView(customize: @escaping (UIScrollView) -> ()) -> some View {
102112
if #available(iOS 14.0, tvOS 14.0, macOS 11.0, *) {

IntrospectTests/UIKitTests.swift

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,42 @@ private struct ListTestView: View {
152152
let spyCell2: () -> Void
153153

154154
var body: some View {
155-
List {
156-
Text("Item 1")
157-
Text("Item 2")
158-
.introspectTableView { tableView in
159-
self.spy2()
160-
}
161-
.introspectTableViewCell { cell in
162-
self.spyCell2()
163-
}
164-
165-
}
166-
.introspectTableView { tableView in
167-
self.spy1()
168-
}
169-
.introspectTableViewCell { cell in
170-
self.spyCell1()
155+
if #available(iOS 16, tvOS 16, macOS 13, *) {
156+
List {
157+
Text("Item 1")
158+
Text("Item 2")
159+
.introspectCollectionView { tableView in
160+
self.spy2()
161+
}
162+
.introspectCollectionViewCell { cell in
163+
self.spyCell2()
164+
}
165+
166+
}
167+
.introspectCollectionView { tableView in
168+
self.spy1()
169+
}
170+
.introspectCollectionViewCell { cell in
171+
self.spyCell1()
172+
}
173+
} else {
174+
List {
175+
Text("Item 1")
176+
Text("Item 2")
177+
.introspectTableView { tableView in
178+
self.spy2()
179+
}
180+
.introspectTableViewCell { cell in
181+
self.spyCell2()
182+
}
183+
184+
}
185+
.introspectTableView { tableView in
186+
self.spy1()
187+
}
188+
.introspectTableViewCell { cell in
189+
self.spyCell1()
190+
}
171191
}
172192
}
173193
}

0 commit comments

Comments
 (0)