Created
May 17, 2019 04:26
-
-
Save agusibrahim/9e6123a3523aff061f69567081b7e472 to your computer and use it in GitHub Desktop.
Ios find active view controller
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
using System; | |
using UIKit; | |
namespace Plugin.Toast | |
{ | |
/// <summary> | |
/// | |
/// </summary> | |
public static class IosHelper | |
{ | |
/// <summary> | |
/// Return currently active and visible controller | |
/// </summary> | |
/// <returns></returns> | |
public static UIViewController GetVisibleViewController() | |
{ | |
try | |
{ | |
var rootController = UIApplication.SharedApplication.KeyWindow.RootViewController; | |
switch (rootController.PresentedViewController) | |
{ | |
case null: | |
return rootController; | |
case UINavigationController controller: | |
return controller.VisibleViewController; | |
case UITabBarController barController: | |
return barController.SelectedViewController; | |
default: | |
return rootController.PresentedViewController; | |
} | |
} | |
catch (Exception) | |
{ | |
return UIApplication.SharedApplication.KeyWindow.RootViewController; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment