Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy

投稿者:

present 元を変更する

Thank you for reading this post, don't forget to subscribe!

let scenes = UIApplication.shared.connectedScenes
let windowScene = scenes.first as? UIWindowScene
guard let window = windowScene?.windows.first else { return nil }
window.rootViewController.rootViewController?.present(vc, animated: true, completion: nil)

古いプロジェクトでは

var baseView = UIApplication.shared.keyWindow?.rootViewController
while ((baseView?.presentedViewController) != nil) {
baseView = baseView?.presentedViewController
}

baseView?.present(alertController, animated: true, completion: nil)