How to find current UIViewController in Xamarin

后端 未结 2 1394
我在风中等你
我在风中等你 2020-12-10 15:54

I am using the Facebook Auth SDK, with a Xamarin Forms C# example. However, the Facebook SDK has depreciated the method and replaced it with one which adds a fromViewC

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-10 16:45

    The accepted answer won´t give you the current view controller if it´s in the stack of a parent UINavigationController, so I came up with the following:

    public static UIViewController GetTopViewController()
    {
        var window = UIApplication.SharedApplication.KeyWindow;
        var vc = window.RootViewController;
        while (vc.PresentedViewController != null)
            vc = vc.PresentedViewController;
    
        if (vc is UINavigationController navController)
            vc = navController.ViewControllers.Last();
    
        return vc;
    }
    

提交回复
热议问题