Skip to content

Instantly share code, notes, and snippets.

@agusibrahim
Created May 17, 2019 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agusibrahim/9e6123a3523aff061f69567081b7e472 to your computer and use it in GitHub Desktop.
Save agusibrahim/9e6123a3523aff061f69567081b7e472 to your computer and use it in GitHub Desktop.
Ios find active view controller
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