swift获取当前活动的viewcontroller

 var topRootViewController = UIApplication.shared.keyWindow?.rootViewController

// 在这里加一个这个样式的循环

 while ((topRootViewController?.presentingViewController) != nil)

 {

          // 这里固定写法

          topRootViewController = topRootViewController?.presentingViewController;

 }

        

原文地址:https://www.cnblogs.com/hualuoshuijia/p/13946986.html