Created
December 21, 2022 08:55
-
-
Save tugcearar/6325b27d60120f9ec97acfb83e182cbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var visibleViewController: UIViewController? { | |
let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate | |
guard let rootViewController = sceneDelegate?.window?.rootViewController else { | |
return nil | |
} | |
return getVisibleViewController(rootViewController) | |
} | |
private func getVisibleViewController(_ rootViewController: UIViewController) -> UIViewController? { | |
if let presentedViewController = rootViewController.presentedViewController { | |
return getVisibleViewController(presentedViewController) | |
} | |
if let navigationController = rootViewController as? UINavigationController { | |
return navigationController.visibleViewController | |
} | |
if let tabBarController = rootViewController as? UITabBarController { | |
return tabBarController.selectedViewController | |
} | |
return rootViewController | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment