在 SwiftUI 中,有几种方法可以呈现像 .popover
这样的模态视图。
我的背景是我想在其他地方而不是在当前视图页面下呈现一个 UIKit 模式视图
private func presentGlobally(animated: Bool, completion: (() -> Void)?) {
var rootViewController = UIApplication.shared.rootViewController
while true {
if let presented = rootViewController?.presentedViewController {
rootViewController = presented
} else if let navigationController = rootViewController as? UINavigationController {
rootViewController = navigationController.visibleViewController
} else if let tabBarController = rootViewController as? UITabBarController {
rootViewController = tabBarController.selectedViewController
} else {
break
}
}
UIApplication.shared.rootViewController?.present(self, animated: animated, completion: completion)
}
上述方法不起作用,因为 SwiftUI 给出了错误
``[演示]尝试本
所以我在想
答案 0 :(得分:1)
在您的算法中,您正在计算 rootViewController
,然后将 self
呈现给 UIApplication.shared.rootViewController
,而不是找到的 rootViewController
替换
UIApplication.shared.rootViewController?.present(self, animated: animated, completion: completion)
与
rootViewController?.present(self, animated: animated, completion: completion)