Introduction:
Some times we may get a situation where we need to present a UINavigationController modally and then push UIViewControllers within the UINavigationController. This required to start with the key window's root view controller and then find the most-presented view controller.
Solution:
Depending on the type of window you have loaded getting the RootViewController can be problematic. This version has been the most stable one I've tried thus far and avoids tail recursive loops.
public UIViewController GetRootController()
{
var root = UIApplication.SharedApplication.KeyWindow.RootViewController;
while (true)
{
switch (root)
{
case UINavigationController navigationController:
root = navigationController.VisibleViewController;
continue;
case UITabBarController uiTabBarController:
root = uiTabBarController.SelectedViewController;
continue;
}
if (root.PresentedViewController == null) return root;
root = root.PresentedViewController;
}
}
Usage:
Some times we may get a situation where we need to present a UINavigationController modally and then push UIViewControllers within the UINavigationController. This required to start with the key window's root view controller and then find the most-presented view controller.
Solution:
Depending on the type of window you have loaded getting the RootViewController can be problematic. This version has been the most stable one I've tried thus far and avoids tail recursive loops.
Usage:
- var presentRootController = GetRootController();
- presentRootController?.PresentViewController(new UINavigationController(uiViewControllerObj), true, null);
FeedBack Note: Please share your thoughts, what you think about this post, Is this post really helpful for you? I always welcome if you drop comments on this post and it would be impressive.
Hey Subbu, its working, I am facing an issue with this and looking for solution now solved, thank you very much.
ReplyDeleteThank you so much Bro..
ReplyDeleteIts Working for me
I have a small issue ..
ReplyDeleteits not showing "Done" button for Close the Image
Please share your Solution
I have a small issue with this
ReplyDeleteafter image opens "Done" button not showing for close the image
Should check your GetRootController method, it doesn't always return a value.
ReplyDelete