Skip to content

Commit c38e70e

Browse files
authored
Add .introspectSplitViewController() on iOS (#89)
1 parent f4f5e32 commit c38e70e

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Changelog
33

44
## master
55

6+
- Added `.introspectSplitViewController()` on iOS
67
- Fixed iPad tests
78
- Added iPad to CI
89
- Added `.introspectColorWell()` on iOS and macOS

Introspect/Introspect.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public enum Introspect {
4848
return typed
4949
}
5050
}
51-
return nil
51+
return root as? AnyViewControllerType
5252
}
5353

5454
/// Finds a subview of the specified type.

Introspect/ViewExtensions.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ extension View {
4545
))
4646
}
4747

48+
/// Finds a `UISplitViewController` from a `SwiftUI.NavigationView` with style `DoubleColumnNavigationViewStyle`.
49+
public func introspectSplitViewController(customize: @escaping (UISplitViewController) -> ()) -> some View {
50+
return inject(UIKitIntrospectionViewController(
51+
selector: { introspectionViewController in
52+
53+
// Search in ancestors
54+
if let splitViewController = introspectionViewController.splitViewController {
55+
return splitViewController
56+
}
57+
58+
// Search in siblings
59+
return Introspect.previousSibling(containing: UISplitViewController.self, from: introspectionViewController)
60+
},
61+
customize: customize
62+
))
63+
}
64+
4865
/// Finds the containing `UIViewController` of a SwiftUI view.
4966
public func introspectViewController(customize: @escaping (UIViewController) -> ()) -> some View {
5067
return inject(UIKitIntrospectionViewController(

IntrospectTests/UIKitTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ private struct NavigationTestView: View {
4949
}
5050
}
5151

52+
@available(iOS 13.0, tvOS 13.0, macOS 10.15.0, *)
53+
private struct SplitNavigationTestView: View {
54+
let spy: () -> Void
55+
var body: some View {
56+
NavigationView {
57+
VStack {
58+
EmptyView()
59+
}
60+
}
61+
.navigationViewStyle(DoubleColumnNavigationViewStyle())
62+
.introspectSplitViewController { navigationController in
63+
self.spy()
64+
}
65+
}
66+
}
67+
5268
@available(iOS 13.0, tvOS 13.0, macOS 10.15.0, *)
5369
private struct ViewControllerTestView: View {
5470
let spy: () -> Void
@@ -477,6 +493,16 @@ class UIKitTests: XCTestCase {
477493
}
478494

479495
#if os(iOS)
496+
func testSplitNavigation() {
497+
498+
let expectation = XCTestExpectation()
499+
let view = SplitNavigationTestView(spy: {
500+
expectation.fulfill()
501+
})
502+
TestUtils.present(view: view)
503+
wait(for: [expectation], timeout: TestUtils.Constants.timeout)
504+
}
505+
480506
func testRootNavigation() {
481507

482508
let expectation = XCTestExpectation()

0 commit comments

Comments
 (0)