How to find topmost view controller on iOS
iOS 4 introduced the rootViewController property on UIWindow:
[UIApplication sharedApplication].keyWindow.rootViewController;
You'll need to set it yourself after you create the view controller though.
get top visible view controller in objective c iOS
Finally i resolve this issue by adding more code. like this...
UINavigationController *vc = SharedAppDelegate.navigationController;
NSLog(@"%@",vc.viewControllers);
UIViewController* controller = vc.viewControllers.firstObject;
NSLog(@"%@",controller.childViewControllers);
UIViewController* carbonContr = controller.childViewControllers.firstObject;
Get top most UIViewController
presentViewController
shows a view controller. It doesn't return a view controller. If you're not using a UINavigationController
, you're probably looking for presentedViewController
and you'll need to start at the root and iterate down through the presented views.
if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
// topController should now be your topmost view controller
}
For Swift 3+:
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
// topController should now be your topmost view controller
}
For iOS 13+
let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
if var topController = keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
// topController should now be your topmost view controller
}
How to check if the top most view controller is an ImagePickerController
You are setting ImagePickerController.self
but the class name is UIImagePickerController
You can use like this
if let imagePicker = UIApplication.shared.windows.first?.topViewController() as? UIImagePickerController {
// Do your stuf
}
Or
if topVC.isKind(of: UIImagePickerController.self) {
// ...
}
Note: By using this you can not cast the top view controller as a UIImagePickerController
. As it's designed by apple.
You can use this and access the view controller by this.
if let pickerHostClass = NSClassFromString("PUPhotoPickerHostViewController"), topVC.isKind(of: pickerHostClass) {
topVC.view.alpha = 0.5
}
How to find topmost view controller on iOS
iOS 4 introduced the rootViewController property on UIWindow:
[UIApplication sharedApplication].keyWindow.rootViewController;
You'll need to set it yourself after you create the view controller though.
Get the top ViewController in iOS Swift
Amit89 brought a way to a solution up. You have to call the .window
property of the AppDelegate.
So I changed the Swift code from the link below to work as intended to find the topmost ViewController. Make sure, that the view is already in the view hierarchy. So this method cannot be called from a .viewDidLoad
Extension to find the topmost ViewController*
extension UIApplication {
class func topViewController(base: UIViewController? = (UIApplication.sharedApplication().delegate as! AppDelegate).window?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
This code originated from GitHub user Yonat in a comment to an objectiveC equivalent. I only changed the bits of code to get it to work without the .keyWindow
property
How to find topmost view controller on iOS
iOS 4 introduced the rootViewController property on UIWindow:
[UIApplication sharedApplication].keyWindow.rootViewController;
You'll need to set it yourself after you create the view controller though.
Get visibleViewController using UIWindow?
IN swift3:
func getVisibleViewController(_ rootViewController: UIViewController?) -> UIViewController? {
var rootVC = rootViewController
if rootVC == nil {
rootVC = UIApplication.shared.keyWindow?.rootViewController
}
if rootVC?.presentedViewController == nil {
return rootVC
}
if let presented = rootVC?.presentedViewController {
if presented.isKind(of: UINavigationController.self) {
let navigationController = presented as! UINavigationController
return navigationController.viewControllers.last!
}
if presented.isKind(of: UITabBarController.self) {
let tabBarController = presented as! UITabBarController
return tabBarController.selectedViewController!
}
return getVisibleViewController(presented)
}
return nil
}
Related Topics
Airpods Not Working as an Input Source for Voice Recorder App
Swift - Json Error: the Data Couldn'T Be Read Because It Isn'T in the Correct Format
Swift: Reload a View Controller
Redirect to Application If Installed, Otherwise to App Store
Detect Tap on a Button in Uitableviewcell for Uitableview Containing Multiple Sections
How to Adjust Font Size to Fit Height and Width of Uilabel
Swift Parse Json - the Data Couldn'T Be Read Because It Isn'T in the Correct Format
Ansible Regex_Findall Multiple Strings
How to Increment the Filename If File Already Exists
How to Develop For Iphone Using a Windows Development Machine
Uidevice Uniqueidentifier Deprecated - What to Do Now
How to Load an Http Url With App Transport Security Enabled in iOS 9
What's the Difference Between the Atomic and Nonatomic Attributes
How to Enable Native Resolution For Apps on Iphone 6 and 6 Plus
How to Use Nsurlconnection to Connect With Ssl For an Untrusted Cert
Getting the Difference Between Two Dates (Months/Days/Hours/Minutes/Seconds) in Swift