When i follow article How To Create A Swift App Without Storyboard to learn how to create an iOS app without using storyboard, after i remove the Main.storyboard file and run it. I meet error message like this Could not find a storyboard named ‘Main’ in bundle NSBundle, below is the full error messages.
2019-10-24 21:01:02.763498+0800 iOSAppUseXibFiles[4524:133328] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Main' in bundle NSBundle
Finally i find out how to resolve this error. It is because my Xcode is upgraded to version 11.0. And when i use this version’s Single View App template to create an iOS Xcode project, it will create a SceneDelegate.swift file.
And the template also add some configuration data in the project’s Info.plist file like below picture. Click Application Scene Manifest —> Scene Configuration —> Application Session Role —> Item 0 (Default Configuration) to expand it, click the minus( -
) button at the end of the Storyboard Name item to remove it, because it’s value is Main, this item means the scene delegate need Main.storyboard file, but we had remove the Main.storyboard file, so the error is thrown.
Now when you run your project, the error will disappear, but you will find there is only a black screen in the iOS simulator. You should follow the article How To Set Application Root View Controller Programmatically In Xcode 11 to know how to add root view controller in the scene delegate class.
But because the Main.storyboard file has been removed, so you should use below source code in SceneDelegate.swift file’s func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) function. Below source code will change the app’s default scene’s background color to red.
var window: UIWindow? func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { print("Scene willConnectTo.") // Force convert UIScene type variable to UIWindowScene type variable. let windowScene:UIWindowScene = scene as! UIWindowScene; // Create the UIWindow variable use above UIWindowScene variable. self.window = UIWindow(windowScene: windowScene) // Set this scene's window's background color. self.window!.backgroundColor = UIColor.red // Create a ViewController object and set it as the scene's window's root view controller. self.window!.rootViewController = ViewController() // Make this scene's window be visible. self.window!.makeKeyAndVisible() guard let _ = (scene as? UIWindowScene) else { return } }
Below is the screen when the example code is executed.