【问题标题】:black screen with sceneDelegate when using storyboard使用情节提要时带有sceneDelegate的黑屏
【发布时间】:2023-03-18 12:22:01
【问题描述】:

首先显示 AuthViewController(又名 root) 然后是带有 tabBarController 的黑屏,但上面没有项目。

可能是什么问题?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.windowScene = windowScene

        if let user = Auth.auth().currentUser {
            FirestoreServices.shared.getUserData(user: user) { (result) in
                switch result {
                case .success(let muser):
                    let mainTabBar = MainTabBarViewController()
                    mainTabBar.currentUser = muser
                    mainTabBar.modalPresentationStyle = .fullScreen
                    self.window?.rootViewController = mainTabBar
                case .failure(_):
                    self.window?.rootViewController = AuthViewController()
                }
            }
        } else {
            self.window?.rootViewController = AuthViewController()
        }
        window?.makeKeyAndVisible()
    } 

【问题讨论】:

    标签: ios swift xcode uiscenedelegate


    【解决方案1】:

    编辑:您需要从情节提要实例化,因为您正在使用情节提要。 你需要用windowScene初始化你的window变量window。

    var window: UIWindow?
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
            guard let windowScene = (scene as? UIWindowScene) else { return }
    
            window = UIWindow(windowScene: windowScene)
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            if let user = Auth.auth().currentUser {
                FirestoreServices.shared.getUserData(user: user) { (result) in
                    switch result {
                    case .success(let muser):
                        let mainTabBar = storyboard.instantiateViewController(withIdentifier: "viewControllerIDInStoryboard") as! TabViewController
                        mainTabBar.currentUser = muser
                        mainTabBar.modalPresentationStyle = .fullScreen
                        self.window?.rootViewController = mainTabBar
                        print("BLA BLA \(String(describing: self.window?.frame))")
                    case .failure(_):
                        let authVC = storyboard.instantiateViewController(withIdentifier: "viewControllerIDInStoryboard") as! AuthViewController
                        self.window?.rootViewController = authVC
                    }
                }
            } else {
                let authVC = storyboard.instantiateViewController(withIdentifier: "viewControllerIDInStoryboard") as! AuthViewController
                self.window?.rootViewController = authVC
            }
            window?.makeKeyAndVisible()
        } 
    

    【讨论】:

    • 我更新了帖子中的代码你的回答对我没有帮助(
    • 如果您不使用情节提要,这可以工作,但我需要使用情节提要
    • 如果您使用情节提要,第一个代码将起作用。我刚刚测试了它。确保情节提要中没有任何设置为初始视图控制器的视图控制器(没有箭头应该指向它)
    • 从情节提要中移除箭头,但问题并没有消失,现在它显示黑屏,半秒后显示带有TabBar的黑屏
    • 好的,我已经更新了我的答案。如果您使用故事板,则需要从故事板实例化您的 ViewController。
    猜你喜欢
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 2015-06-07
    • 2013-05-02
    • 1970-01-01
    相关资源
    最近更新 更多