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 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.
Getting the top most UIViewController
You want visibleViewController
:
The currently visible view can belong either to the view controller at the top of the navigation stack or to a view controller that was presented modally.
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;
How to get the current visible viewController from AppDelegate
Do conditional casting on the return value to safely check its type.
if let currentVC = UIApplication.topViewController() as? MyViewController {
//the type of currentVC is MyViewController inside the if statement, use it as you want to
}
Your whole function implementation is flawed, if it actually worked, it would lead to infinite recursion. Once you find out the type of your current top view controller in your if statements, you are calling the same function again with the current root controller as its input value. Your function only ever exists, if it reaches either a call from a view controller, whose class is none of the ones specified in your optional bindings.
Moreover, your whole implementation doesn't do anything at the moment. You find out the type of your root view controller, but than you upcast it by returning a value of type UIViewController
.
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.
Related Topics
Inter-App Data Migration (Migrating Data to New App Version)
How Does String Substring Work in Swift
Accessing an Enumeration Association Value in Swift
Get Class Name of Object as String in Swift
Use Binding≪Int≫ With a Textfield Swiftui
What Is the Cause of This Type Error
Usb Connection Delegate on Swift
Remove or Edit User Location Blue Pulsing Circle
Wait Until Swift For Loop With Asynchronous Network Requests Finishes Executing
Is Key-Value Observation (Kvo) Available in Swift
How to Compare Enum With Associated Values by Ignoring Its Associated Value in Swift
How to Round a Double to the Nearest Int in Swift
Nsuserdefaults Not Working on Xcode Beta With Watch Os2