Skip to content

Commit

Permalink
Merge pull request #27 from 3sidedcube/hotfix/visibleViewController-a…
Browse files Browse the repository at this point in the history
…mbiguous

Remove `visibleViewController` from `UIViewController` extension to prevent ambiguity with `navigationController`
  • Loading branch information
BenShutt authored Oct 2, 2020
2 parents 423a593 + c06a385 commit 7bd6719
Showing 1 changed file with 13 additions and 12 deletions.
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)
}
}

0 comments on commit 7bd6719

Please sign in to comment.