Skip to content

Add .introspectSplitViewController() on iOS #89

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 5 commits into from
Mar 23, 2021
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
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog

## master

- Added `.introspectSplitViewController()` on iOS
- Fixed iPad tests
- Added iPad to CI
- Added `.introspectColorWell()` on iOS and macOS
Expand Down
2 changes: 1 addition & 1 deletion Introspect/Introspect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public enum Introspect {
return typed
}
}
return nil
return root as? AnyViewControllerType
}

/// Finds a subview of the specified type.
Expand Down
17 changes: 17 additions & 0 deletions Introspect/ViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ extension View {
))
}

/// Finds a `UISplitViewController` from a `SwiftUI.NavigationView` with style `DoubleColumnNavigationViewStyle`.
public func introspectSplitViewController(customize: @escaping (UISplitViewController) -> ()) -> some View {
return inject(UIKitIntrospectionViewController(
selector: { introspectionViewController in

// Search in ancestors
if let splitViewController = introspectionViewController.splitViewController {
return splitViewController
}

// Search in siblings
return Introspect.previousSibling(containing: UISplitViewController.self, from: introspectionViewController)
},
customize: customize
))
}

/// Finds the containing `UIViewController` of a SwiftUI view.
public func introspectViewController(customize: @escaping (UIViewController) -> ()) -> some View {
return inject(UIKitIntrospectionViewController(
Expand Down
26 changes: 26 additions & 0 deletions IntrospectTests/UIKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ private struct NavigationTestView: View {
}
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15.0, *)
private struct SplitNavigationTestView: View {
let spy: () -> Void
var body: some View {
NavigationView {
VStack {
EmptyView()
}
}
.navigationViewStyle(DoubleColumnNavigationViewStyle())
.introspectSplitViewController { navigationController in
self.spy()
}
}
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15.0, *)
private struct ViewControllerTestView: View {
let spy: () -> Void
Expand Down Expand Up @@ -477,6 +493,16 @@ class UIKitTests: XCTestCase {
}

#if os(iOS)
func testSplitNavigation() {

let expectation = XCTestExpectation()
let view = SplitNavigationTestView(spy: {
expectation.fulfill()
})
TestUtils.present(view: view)
wait(for: [expectation], timeout: TestUtils.Constants.timeout)
}

func testRootNavigation() {

let expectation = XCTestExpectation()
Expand Down