iOS 14:在 SwiftUI @main 中设置应用程序根视图控制器后场景生命周期事件未被触发

iOS 14: Scene lifecycle events not getting hit after setting the application root view controller in SwiftUI @main

我的测试代码:

import SwiftUI

@main
struct TestingIOS14App: App {
    @Environment(\.scenePhase) var scenePhase
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .withHostingWindow { window in
                    let hostingController = UIHostingController(rootView: ContentView())
                    let mainNavigationController = UINavigationController(rootViewController: hostingController)
                    mainNavigationController.navigationBar.isHidden = true
                    window?.rootViewController = mainNavigationController
                }
        }
        .onChange(of: scenePhase) { newScenePhase in
            switch newScenePhase {
            case .active:
                print("App is active")
            case .inactive:
                print("App is inactive")
            case .background:
                print("App is in background")
            @unknown default:
                print("Oh - interesting: I received an unexpected new value.")
            }
        }
    }
}

extension View {
    func withHostingWindow(_ callback: @escaping (UIWindow?) -> Void) -> some View {
        self.background(HostingWindowFinder(callback: callback))
    }
}

struct HostingWindowFinder: UIViewRepresentable {
    var callback: (UIWindow?) -> Void

    func makeUIView(context: Context) -> UIView {
        let view = UIView()
        DispatchQueue.main.async { [weak view] in
            self.callback(view?.window)
        }
        return view
    }

    func updateUIView(_ uiView: UIView, context: Context) { }
}

@Asperi 的回答 here 和上面的代码似乎有效,但场景生命周期在 .onChange(of: 似乎没有达到 除了 最初启动应用程序时的 active 阶段。不确定我做错了什么,请提供任何帮助。

非常感谢:)

您的代码与您 link 的答案之间的区别在于您完全替换了应用程序的视图层次结构。注意他们如何使用

if let controller = window?.rootViewController

相反,您分配了一个新的根视图控制器

window?.rootViewController = mainNavigationController

我猜这就是导致您出现问题的原因。

不过,如果您只想隐藏导航栏,可以使用 SwiftUI 方法来实现。