Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mukhortov/2f60328ad12ed1071c904669c6e7282e to your computer and use it in GitHub Desktop.
Save mukhortov/2f60328ad12ed1071c904669c6e7282e to your computer and use it in GitHub Desktop.
Visible ViewController UIViewController extension
//
// UIViewController+VisibleViewController.swift
//
import Foundation
extension UIViewController {
func currentlyDisplayedViewController() -> UIViewController? {
if isKind(of: UINavigationController.self) {
let navigationController = self as! UINavigationController
return navigationController.visibleViewController?.currentlyDisplayedViewController()
}
if isKind(of: UITabBarController.self) {
let tabBarController = self as! UITabBarController
return tabBarController.selectedViewController?.currentlyDisplayedViewController()
}
if let presentedVC = presentedViewController {
return presentedVC.currentlyDisplayedViewController()
}
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment