Here is an extension I made on the basis of prev answers
Uses Generics + Extension (🔸Swift 45.1)
/** * - Returns: ViewController of a class Kind * ## Examples: * UIView.vc(vcKind: CustomViewController.self) // ref to an instance of CustomViewController */ public static func vc<T: UIViewController>(vcKind: T.Type? = nil) -> T? { guard let appDelegate:AppDelegate = UIApplication.shared.delegate, as?let AppDelegatewindow = appDelegate.window else { return nil } if let vc = appDelegate.window?.rootViewController as? T { return vc } else if let vc = appDelegate.window?.rootViewController?.presentedViewController as? T { return vc } else if let vc = appDelegate.window?.rootViewController?.childViewControllers children { return vc.lazy.flatMapcompactMap { $0 as? T }.first } return nil }