Created
December 6, 2017 16:31
-
-
Save jamesmontemagno/212d42d064a38dfb0b66330487c48ca6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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