Set rootViewController iOS 13
This is because AppDelegate doesn't have window
property anymore.
Now you must use SceneDelegate's scene(_:willConnectTo:options:)
method to change root view controller.
Like shown in this example:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
// Instantiate UIWindow with scene
let window = UIWindow(windowScene: scene)
// Assign window to SceneDelegate window property
self.window = window
// Set initial view controller from Main storyboard as root view controller of UIWindow
self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
// Present window to screen
self.window?.makeKeyAndVisible()
}
Set rootViewController programmatically not working, picking initial view controller from storyboard only in Xcode 11
Got the answer by doing some more google, i mean stackoverflow.
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
let vc = storyboard.instantiateViewController (withIdentifier: "Primary") as! ViewController
window = UIWindow(windowScene: windowScene)
window?.rootViewController = vc
window?.makeKeyAndVisible()
}
SceneDelegate is used for this purpose since iOS 13 not AppDelegate. Here is the link SceneDelegate example
Why is manually setup root view controller showing black screen?
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.
How to set root view controller programmatically in Xcode 13 with SpriteKit?
If you want to delete the Main.Storyboard, you need to add the following to your AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let viewController = GameViewController()
window?.rootViewController = viewController
window?.makeKeyAndVisible()
return true
}
And in GameViewController.swift you need to add the following:
override func loadView() {
self.view = SKView(frame: UIScreen.main.bounds)
}
set initial viewcontroller in appdelegate - swift
Xcode11 and SceneDelegate note:
Starting from Xcode11, because of SceneDelegates, it's likely that you shouldn't do it inside AppDelegate
. Instead do it from SceneDelegate
. For more on that see this other answer
Old answer:
I used this thread to help me convert the objective C to swift, and its working perfectly.
Instantiate and Present a viewController in Swift
Swift 2 code:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginSignupVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
Swift 3 code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
Related Topics
Xcode 9 - "Fixed Width Constraints May Cause Clipping" and Other Localization Warnings
Dynamic Cell Width of Uicollectionview Depending on Label Width
Swift 3.0 Error: Escaping Closures Can Only Capture Inout Parameters Explicitly by Value
Change the Uitableviewcell Height According to Amount of Text
How to Reset the iOS Simulator from the Command Line
Change Tab Bar Item Selected Color in a Storyboard
Iphone: Hide Uitableview Search Bar by Default
Select Multiple Images (Uiimagepickercontroller or Photos.App Share Ui)
Swift 2.0 Calendar Components Error
iOS 7 App Icons, Launch Images and Naming Convention While Keeping iOS 6 Icons
Xcode 8 - Ib Designables - Failed to Render and Update Auto Layout Status, the Agent Crashed
Uitableview and Uiview with Keyboardwillshow
Iad Is Shutting Down. Should I Remove the Iad Framework from All My Applications
(Swift) How to Print "\" Character in a String
iOS Uinavigationbar Button Remains Faded After Segue Back
What Is the Meaning of the "No Index Path for Table Cell Being Reused" Message in iOS 6/7