gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:12:41 25 4
gpt4 key购买 nike

我正在尝试在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com