Skip to content

Added introspect for scroll view in TabBarView with PageTabViewStyle #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion Introspect/ViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,23 @@ extension View {
return introspect(selector: TargetViewSelector.siblingContainingOrAncestor, customize: customize)
}
}


/// Finds the horizontal `UIScrollView` from a `SwiftUI.TabBarView` with tab style `SwiftUI.PageTabViewStyle`.
///
/// Customize is called with a `UICollectionView` wrapper, and the horizontal `UIScrollView`.
@available(iOS 14.0, tvOS 14.0, watchOS 7.0, *)
@available(macOS, unavailable)
public func introspectPagedTabView(customize: @escaping (UICollectionView, UIScrollView) -> ()) -> some View {
return introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: { (collectionView: UICollectionView) in
for subview in collectionView.subviews {
if NSStringFromClass(type(of: subview)).contains("EmbeddedScrollView"), let scrollView = subview as? UIScrollView {
customize(collectionView, scrollView)
break
}
}
})
}

/// Finds a `UITextField` from a `SwiftUI.TextField`
public func introspectTextField(customize: @escaping (UITextField) -> ()) -> some View {
introspect(selector: TargetViewSelector.siblingContainingOrAncestorOrAncestorChild, customize: customize)
Expand Down
39 changes: 39 additions & 0 deletions IntrospectTests/UIKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ private struct TabRootTestView: View {
}
}

@available(iOS 14.0, tvOS 14.0, watchOS 7.0, *)
@available(macOS, unavailable)
private struct PageTabViewStyleTestView: View {

let spy: (UICollectionView, UIScrollView) -> Void

var body: some View {
TabView {
Text("Item 1")
.tag(0)
}
.tabViewStyle(PageTabViewStyle())
.introspectPagedTabView { collectionView, scrollView in
spy(collectionView, scrollView)
}
}
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15.0, *)
private struct ListTestView: View {

Expand Down Expand Up @@ -576,6 +594,27 @@ class UIKitTests: XCTestCase {
TestUtils.present(view: view)
wait(for: [expectation], timeout: TestUtils.Constants.timeout)
}

@available(iOS 14.0, tvOS 14.0, watchOS 7.0, *)
func testPagedTabView() throws {

var collectionView1: UICollectionView?
var scrollView1: UIScrollView?

let expectation = XCTestExpectation()
let view = PageTabViewStyleTestView(spy: { collectionView, scrollView in
collectionView1 = collectionView
scrollView1 = scrollView
expectation.fulfill()
})
TestUtils.present(view: view)
wait(for: [expectation], timeout: TestUtils.Constants.timeout)

let unwrappedCollectionView = try XCTUnwrap(collectionView1)
let unwrappedScrollView = try XCTUnwrap(scrollView1)

XCTAssertTrue(unwrappedCollectionView.subviews.contains(where: { $0 === unwrappedScrollView }))
}
#endif
}
#endif