mbradber
mbradber

Reputation: 505

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

With a very basic single view application, I've deleted the main storyboard file and removed any references to it. As such I'm setting the window rootViewController programmatically. However, while this displays the single view (containing a label) correctly in simulator, it displays a black screen when running it on device. Here is the only code for the app.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = DummyViewController()
        window?.makeKeyAndVisible()
        return true
    }

I've removed the entry for Main storyboard from the info.plist file, as well as the 'Main Interface' entry in the General settings.

I'm using Swift 3 and targeting an iOS 8 device. I'm using XCode 8.3.1.

There is no output in the console, and there are no exceptions. The viewDidLoad function is even triggering on breakpoint, so the codepath seems to be running correctly.

Any ideas?

Here's the bare bones code for DummyViewController upon request.

class DummyViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

General settings showing no reference to Main Interface enter image description here

Here is the image for the .xib linked to DummyViewController enter image description here

** The solution to get around this case is to manually specify the .xib to load for the DummyViewController **

Upvotes: 8

Views: 7217

Answers (4)

Manoj G.
Manoj G.

Reputation: 62

You just need to initialize viewcontroller object and implement white background color to its instance.

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)
    //Add below 2 lines
    let vc = DummyViewController()
    vc.view.backgroundColor = .white

    window?.rootViewController = vc
    window?.makeKeyAndVisible()
    return true
}

Upvotes: 0

Lalit kumar
Lalit kumar

Reputation: 2207

change Your window root as and set color

let viewController:DummyViewController = DummyViewController()
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = viewController

And change your controller

  override func viewDidLoad() {
    self.view.backgroundColor = UIColor.white
  }

Upvotes: 1

Carter
Carter

Reputation: 3093

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)

Upvotes: 3

Nischal Hada
Nischal Hada

Reputation: 3288

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
    }

Upvotes: 2

Related Questions