Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is manually setup root view controller showing black screen?

I have manually setup a root view controller for iOS 13 using Xcode 11, Beta 5. Deleted references to main in the deployment info including removing reference to main in info.plist which I never found myself having to do prior to iOS 13. Setup for window is done in SceneDelegate, nested in willConnectTo function. Normally the app would crash if I missed a step. Now I'm getting a blank black screen instead of seeing what my view controller is setup for, say a red background. All of this use to work prior to beta 5.

Have performed erase all content and settings on the simulator. Cleared the build folder and have ran the app on a physical device. Also have used another computer with Xcode 11, beta 5. All results to the same blank black screen. What am I missing?

Here is my manual setup for root view controller in SceneDelegate file nested in willConnectTo function:

let viewCon = ViewController()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = viewCon
window?.makeKeyAndVisible()
like image 383
Adnan Avatar asked Aug 11 '19 15:08

Adnan


2 Answers

To ensure you see your root view controller in iOS 13 when everything is done programmatically, you must do the following:

In the scene delegate, you must create the window instance and the root view controller:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let winScene = (scene as? UIWindowScene) else { return }

        // Create the root view controller as needed
        let vc = ViewController()
        let nc = UINavigationController(rootViewController: vc)

        // Create the window. Be sure to use this initializer and not the frame one.
        let win = UIWindow(windowScene: winScene) 
        win.rootViewController = nc
        win.makeKeyAndVisible()
        window = win
    }
}

Your Info.plist has to have the "Application Scene Manifest" entry. Below it should be the "Enable Multiple Windows" entry. Set to YES or NO as appropriate to your app. Optionally you should also have the "Scene Configuration" entry.

All of these entries are added by Xcode when you check the "Supports multiple windows" setting on the General tab of your target. This will default the "Enable Multiple Windows" entry to YES so you can change that to NO if you want scenes but not multiple windows.

like image 160
rmaddy Avatar answered Nov 15 '22 09:11

rmaddy


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    guard let windowScene = (scene as? UIWindowScene) else { return }

    let window = UIWindow(windowScene: windowScene)
    self.window = window
    let mainstoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let newViewcontroller:UIViewController = mainstoryboard.instantiateViewController(withIdentifier: "YourVCName") as! YourVCName
    navigationController = UINavigationController(rootViewController: newViewcontroller)
    window.rootViewController = navigationController
    window.makeKeyAndVisible()
}

try this code!! if you are using ios 13 and your xcode is updated then you should set your root view controller in scene delegate instead of app delegate.

like image 24
Vipin Pareek Avatar answered Nov 15 '22 07:11

Vipin Pareek