iOS - Setting 'rootViewController' results in black screen on device only

It looks like the ViewController is not set to display anything. Unless you are using a xib (in which case you would need to load the view controller in a different way, see below), there is nothing describing how the ViewController's view should render.

To test this out, you can add the line self.view.backgroundColor = UIColor.red to the ViewController's viewDidLoad() method, then run it again on the device- if the background color turns red, then hooray! The next step will be programmatically adding a UILabel.


Loading UIViewController From a XIB

let vc = MyViewController(nibName: "xibname", bundle: nil)

Alternatively, you can mask the loading by adding a custom init inside MyViewController:

class MyViewController: UIViewController {
    required init() {
        super.init(nibName: "xibname", bundle: nil)
    }
}

(Thank you zonily-jame for the addition of hiding it in the class)


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

         let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
         let loginView : BaseClassVC = mainStoryboardIpad.instantiateViewControllerWithIdentifier("BaseClassVC") as BaseClassVC
            let navigationController = UINavigationController(rootViewController: loginView)

         self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
         self.window?.rootViewController = navigationController
         self.window?.makeKeyAndVisible()

         return true
    }

Tags:

Ios

Swift