Skip to content
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

Remove visibleViewController from UIViewController extension to prevent ambiguity with navigationController #27

Merged
merged 1 commit into from
Oct 2, 2020
Merged
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
25 changes: 13 additions & 12 deletions Sources/Extensions/UIApplication+KeyWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,55 @@ public extension UIApplication {

/// `visibleViewController` on the `rootViewController` of `keyWindow`
var visibleViewController: UIViewController? {
return appKeyWindow?.rootViewController?.visibleViewController
guard let rootViewController = appKeyWindow?.rootViewController else {
return nil
}
return rootViewController.visibleViewController(
from: rootViewController
)
}
}

// MARK: - UIViewController + VisibleViewController

public extension UIViewController {

/// Visible `UIViewController` on `viewController`
/// Visible `UIViewController` from `viewController`
///
/// - Parameters:
/// - viewController: `UIViewController`
/// - includeSystemViewControllers: `Bool` include `UIViewController`s which
/// are presented and return `true` to `isSystemViewController`
func visibleViewController(
_ viewController: UIViewController,
from viewController: UIViewController,
includeSystemViewControllers: Bool = false
) -> UIViewController {

// splitViewController
if let splitViewController = viewController as? UISplitViewController,
let firstViewController = splitViewController.viewControllers.first {
return visibleViewController(firstViewController)
return visibleViewController(from: firstViewController)
}

// navigationController
if let navigationController = viewController as? UINavigationController,
let lastViewController = navigationController.viewControllers.last {
return visibleViewController(lastViewController)
return visibleViewController(from: lastViewController)
}

// tabBarController
if let tabController = viewController as? UITabBarController {
if let selectedViewController = tabController.selectedViewController {
return visibleViewController(selectedViewController)
return visibleViewController(from: selectedViewController)
}
}

// presentedViewController
if let presentedViewController = viewController.presentedViewController,
(includeSystemViewControllers || !presentedViewController.isSystemViewController) {
return visibleViewController(presentedViewController)
return visibleViewController(from: presentedViewController)
}

return viewController
}

/// `visibleViewController(_:)` with `self`
var visibleViewController: UIViewController {
return visibleViewController(self)
}
}