Google Resolved: RootViewController.PresentViewController is not working in Xamarin iOS (C#) | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Wednesday, 27 February 2019

Resolved: RootViewController.PresentViewController is not working in Xamarin iOS (C#)

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 == nullreturn root;  
  •                root = root.PresentedViewController;  
  •            }  
  •        } 

    Usage
    1. var presentRootController = GetRootController();  
    2.             presentRootController?.PresentViewController(new UINavigationController(uiViewControllerObj), truenull);  


    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.

    Follow me always at @Subramanyam_B
    Have a nice day by  :)

    5 comments:

    1. Hey Subbu, its working, I am facing an issue with this and looking for solution now solved, thank you very much.

      ReplyDelete
    2. Thank you so much Bro..
      Its Working for me

      ReplyDelete
    3. I have a small issue ..
      its not showing "Done" button for Close the Image
      Please share your Solution

      ReplyDelete
    4. I have a small issue with this
      after image opens "Done" button not showing for close the image

      ReplyDelete
    5. Should check your GetRootController method, it doesn't always return a value.

      ReplyDelete

    Search Engine Submission - AddMe