Skip to content

Instantly share code, notes, and snippets.

@jamesmontemagno
Created December 6, 2017 16:31
Show Gist options
  • Select an option

  • Save jamesmontemagno/212d42d064a38dfb0b66330487c48ca6 to your computer and use it in GitHub Desktop.

Select an option

Save jamesmontemagno/212d42d064a38dfb0b66330487c48ca6 to your computer and use it in GitHub Desktop.
public class UIHelpers
{
MyPage view;
public UIHelpers()
{
if(!Forms.IsInitialized)
Forms.Init();
view = new MyPage();
}
public void ShowMyPage()
{
var controller = view.CreateViewController();
var vc = GetVisibleViewController();
if (vc.NavigationController != null)
vc.NavigationController.PushViewController(controller, true);
else
vc.PresentViewController(controller, true, () => { });
}
UIViewController GetVisibleViewController()
{
UIViewController viewController = null;
var window = UIApplication.SharedApplication.KeyWindow;
if (window != null && window.WindowLevel == UIWindowLevel.Normal)
viewController = window.RootViewController;
if (viewController == null)
{
window = UIApplication.SharedApplication.Windows.OrderByDescending(w => w.WindowLevel).FirstOrDefault(w => w.RootViewController != null && w.WindowLevel == UIWindowLevel.Normal);
if (window == null)
throw new InvalidOperationException("Could not find current view controller");
else
viewController = window.RootViewController;
}
while (viewController.PresentedViewController != null)
viewController = viewController.PresentedViewController;
var navController = viewController as UINavigationController;
if (navController != null)
viewController = navController.ViewControllers.Last();
return viewController;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment