页面跳转时出现黑屏问题求助:已配置Storyboard ID仍无法正常跳转至LoginViewController
Hey there! Let's break down why you're hitting that black screen when trying to auto-navigate to your LoginViewController for unlogged users. I've dealt with this exact issue before, so here are the most common fixes to check:
1. Verify Your Storyboard ID Setup
- Double-check that the Storyboard ID in the Identity Inspector of your
LoginViewControllermatches exactly what you're using in code (case-sensitive, no typos!). - Make sure you're loading the correct Storyboard file. If your
LoginViewControlleris in a custom Storyboard (notMain.storyboard), you need to reference that specific name when instantiating the VC.
2. Fix the Timing of Your Jump Logic
Calling present or performSegue too early (like in viewDidLoad) can cause a black screen because the current view controller hasn't finished rendering yet. Try these adjustments:
- Move the jump code to
viewDidAppear(_:)instead:override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) guard !isUserLoggedIn else { return } let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") present(loginVC, animated: true) } - If you need to trigger it on app launch, consider replacing the root view controller directly instead of presenting (this is more reliable for startup flows):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { guard !isUserLoggedIn else { return true } let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") window?.rootViewController = loginVC window?.makeKeyAndVisible() return true }
3. Check the LoginViewController Itself
A black screen might not be a jump issue—it could be that your LoginViewController has no visible content:
- Confirm you've set a background color for its root view (default is black, which looks like a black screen if there are no other elements).
- Check if there's blocking code in
viewDidLoad(like synchronous network calls) that's preventing the UI from rendering.
4. Validate Segue Setup (If Using performSegue)
If you're using performSegue(withIdentifier:sender:):
- Ensure you've created a valid segue in Storyboard between the source VC and
LoginViewController, with the exact identifier you're using in code. - Make sure the source VC is actually the one being displayed when the segue is triggered.
内容的提问来源于stack exchange,提问作者user16489264
相关产品推荐
相关产品推荐

