【问题标题】:Issue with presenting viewcontroller in iOS 13, Xcode 11在 iOS 13、Xcode 11 中显示视图控制器的问题
【发布时间】: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


【解决方案1】:

此代码将用于在顶部创建视图控制器。您可以在此行调整视图控制器的大小:popOverVC.view.frame = lSs 我不确定这段代码是否正是您所要求的,但如果您需要快速解决方案,它将在 swift 5、iOS 中显示视图控制器13和xcode 11。注意它是一个子视图控制器,所以如果你删除父视图控制器,它也会离开。只需将 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)

【讨论】:

  • No it doesn't work getting same error "[Window] Manually add the rootViewController's view to the view hierarchy is no longer support. Please allow UIWindow to add the rootViewController's view to the view hierarchy itself" in iOS13
  • 我直接从 ios 13 swift 5 应用程序中复制了代码。我不确定你为什么会收到错误
【解决方案2】:

我在更新之前运行良好的应用程序时收到了同样的错误。看起来 AppDelegate.swift 的新要求是:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    return true
  }

  // MARK: UISceneSession Lifecycle
  func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  }
  func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { }
}

第

【讨论】:

    【解决方案3】:

    今晚我遇到了同样的问题。我尝试了网上人们建议的方法,包括 Stackoverflow,但都没有奏效。

    刚才,好像魔法在起作用,我试图形成一个从 Main.storyboard 中的 ViewController(视觉上是所有子视图的“背景”层)到 ViewController.swift 中的 ViewController 类的出口。它在我的 Xcode 12.2 中的 iOS 13 模拟器项目中工作。

    希望这个技巧也对你有用。

    【讨论】:

      【解决方案4】:

      我发现这可以解决我的问题(在旧的 Objective-C 代码中):

      // old way broken in iOS 13
      //appDelegate.window.rootViewController = vc;
      
      // this works in iOS 13
      [appDelegate.window setRootViewController:vc];
      

      【讨论】:

      • 您确定这能解决问题吗?它应该具有完全相同的效果。
      猜你喜欢
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多