rootViewController 为黑色且不显示任何内容 [英] rootViewController is black and shows no content

查看:25
本文介绍了rootViewController 为黑色且不显示任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个名为 StartViewController 的 VC 在启动时是黑色的(应该是黄色的).谁能帮帮我??

My first VC called StartViewController is just black when launch (should be yellow). Can anyone help me??

这是我的 AppDelegate:

This is my AppDelegate:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?

  func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    //MARK: Start VC
    let navController = UINavigationController(rootViewController: StartViewController())
    navController.navigationBar.isTranslucent = false
    window?.rootViewController = navController
    window?.isOpaque = true
    return true
  }
}

这是我的 StartViewController 类:

This is my StartViewController Class:

import UIKit 
class StartViewController: UIViewController {
    @IBOutlet weak var btn: UIButton!    
    override func viewDidLoad() {
        super.viewDidLoad()
        print("HELLO WORLD")
    }
}

这是我的故事板的样子(后来添加):

This is how my Storyboard looks like (added afterwards):

感谢您的帮助!如果您需要查看其他课程或内容,请询问!:)

Thanks for any help! If you need to see other classes or stuff please ask! :)

推荐答案

您不能像 StartViewController() 那样初始化故事板中的视图控制器.所以这不起作用:

You can't initialize a view controller that's in a storyboard like StartViewController(). So this doesn't work:

let navController = UINavigationController(rootViewController: StartViewController())

相反,您需要使用 instantiateViewController 方法.确保还设置了故事板 ID".在您的视图控制器的故事板中.

Instead, you need to use the instantiateViewController method. Make sure to also set the "Storyboard ID" in the storyboard for your view controller.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerID")
let navController = UINavigationController(rootViewController: viewController)

这篇关于rootViewController 为黑色且不显示任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆