iOS Present modal view controller on startup without flash

前端 未结 9 1155
囚心锁ツ
囚心锁ツ 2020-12-08 00:53

I\'d like to present modally, at first startup, a tutorial wizard to the user.

Is there a way to present a modal UIViewController on application startup

9条回答
  •  死守一世寂寞
    2020-12-08 01:01

    Bruce's upvoted answer in Swift 3:

    if let vc = window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "LOGIN")
        {
            let launch = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()!
            launch.view.frame = vc.view.bounds
            launch.view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
            window?.makeKeyAndVisible()
            window?.addSubview(launch.view)
    
            //Using DispatchQueue to prevent "Unbalanced calls to begin/end appearance transitions"
            DispatchQueue.global().async {
                // Bounce back to the main thread to update the UI
                DispatchQueue.main.async {
                    self.window?.rootViewController?.present(vc, animated: false, completion: {
    
                        UIView.animate(withDuration: 0.5, animations: {
                            launch.view.alpha = 0
                        }, completion: { (_) in
                            launch.view.removeFromSuperview()
                        })
                    })
                }
            }
        }
    

提交回复
热议问题