swift - 在 iOS 13、Xcode 11 中呈现 View Controller 的问题

标签 swift uiviewcontroller appdelegate ios13 xcode11

我正在尝试在 topMostViewController 上呈现一个 View Controller 。它适用于 iOS 12 及更低版本。但是在 iOS 13 上我收到了这个错误:

Manually adding the rootViewController's view to the view hierarchy is no longer supported. Please allow UIWindow to add the rootViewController's view to the view hierarchy itself.

我已经检查了 iOS 12 及更低版本,下面的代码工作正常。但是在 iOS 13 上,我无法呈现 View Controller 。我在 viewDidLoad 上打印;它正在打印,但未显示 View 。

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
}

最佳答案

此代码将用于在顶部创建一个 View Controller 。您可以在此行调整 View Controller 的大小:popOverVC.view.frame = lSs 我不确定这段代码是否正是您所要求的,但如果您需要快速解决方案,它将在 swift 5、iOS 13 和 xcode 11 中呈现 View Controller 。请注意,它是一个 subview Controller ,因此如果您删除父 View Controller ,它也会离开。只需将 self 更改为您想要展示的 ViewController。

    let popOverVC = UIStoryboard(name: "yourSB", bundle: nil).instantiateViewController(withIdentifier: "vcYouWantID") as! vcYouWant
    self.addChild(popOverVC)
    let lSs = UIScreen.main.bounds
    popOverVC.view.frame = lSs
    popOverVC.view.tag = tag
    self.view.addSubview(popOverVC.view)
    popOverVC.didMove(toParent: self)

关于swift - 在 iOS 13、Xcode 11 中呈现 View Controller 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58484573/

相关文章:

swift - 当我创建一个注销的segue时,导航 Controller 突然出现 - Swift

swift 中的 HTML 表解析

swift - Firebase,我如何返回句柄以便我可以调用 removeObserver?

iphone - ZBarReaderViewController、 View Controller 层次结构和其他

ios - 在真正的 iOS 上打开一个新的 View Controller

ios - UITabBarController 以编程方式添加 Controller 导致通知打开时出现问题

ios - 当应用程序因调用移动接收而消失时如何暂停计时器?

xcode - 将异步获取的数据传递给另一个类

ios - 我的 Storyboard 包含我的观点吗?

objective-c - 在 AppDelegate 中存储、访问和编辑字符串