Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

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 **

like image 430
mbradber Avatar asked Apr 17 '17 05:04

mbradber


3 Answers

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)

like image 148
Carter Avatar answered Oct 16 '22 21:10

Carter


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
    }
like image 37
Nischal Hada Avatar answered Oct 16 '22 19:10

Nischal Hada


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
  }
like image 1
Lalit kumar Avatar answered Oct 16 '22 21:10

Lalit kumar