【发布时间】:2019-10-21 10:40:16
【问题描述】:
我正在尝试在 topMostViewController 上展示一个视图控制器。它适用于 iOS 12 及更低版本。但是在 iOS 13 上我收到了这个错误:
不再支持手动将 rootViewController 的视图添加到视图层次结构中。请允许 UIWindow 将 rootViewController 的视图添加到视图层次本身。
我检查了 iOS 12 及更低版本,以下代码运行良好。但在 iOS 13 上,我无法呈现视图控制器。我打印在viewDidLoad;它正在打印,但视图没有出现。
func presentInWindow(animated flag: Bool = true, completion: (() -> Void)? = nil) {
DispatchQueue.main.async {
var alertWindow: UIWindow?
alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow?.windowLevel = UIWindow.Level.alert + 1
alertWindow?.rootViewController = UIApplication.topViewController()
if let rootViewController = alertWindow?.rootViewController {
alertWindow?.makeKeyAndVisible()
rootViewController.present(self, animated: flag, completion: completion)
}
}
}
static func topViewController() -> UIViewController? {
var topViewController: UIViewController?
if #available(iOS 13.0, *) {
topViewController = shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first?.rootViewController
} else {
topViewController = shared.delegate?.window??.rootViewController
}
while true {
if let presented = topViewController?.presentedViewController {
topViewController = presented
} else if let nav = topViewController as? UINavigationController {
topViewController = nav.visibleViewController
} else {
break
}
}
return topViewController
}
【问题讨论】:
-
我的问题是'rootViewController.present(self,animated:flag,completion:completion)',当我尝试展示vc时。我收到错误“[Window] 不再支持手动将 rootViewController 的视图添加到视图层次结构。请允许 UIWindow 将 rootViewController 的视图添加到视图层次结构本身”
标签: swift uiviewcontroller appdelegate ios13 xcode11