Skip to content

Instantly share code, notes, and snippets.

@richimf
Last active August 13, 2020 15:06
Show Gist options
  • Save richimf/89f52a46ba08fd22ebdf911a40761088 to your computer and use it in GitHub Desktop.
Save richimf/89f52a46ba08fd22ebdf911a40761088 to your computer and use it in GitHub Desktop.
Using Storyboard programmatically as main storyboard
/**
AppDelegate.swift
In Project settings set the "Main Interface" text field to empty.
With the Interface Builder implementation,
the app launches the initial view controller of the storyboard set in the target’s Main Interface.
To do something similar in code, you need to take a different approach.
Open AppDelegate.swift and replace the code inside application(_:didFinishLaunchingWithOptions:) with the following:”
*/
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// Initialize the storyboard in code using the storyboard name
let storyboard = UIStoryboard(name: "TabBar", bundle: nil)
// Create a reference to the storyboard's initial view controller
let viewController = storyboard.instantiateInitialViewController()
// Set the app delegate's `window` using the device's screen size as the `frame`
window = UIWindow(frame: UIScreen.main.bounds)
// Set the window's root view controller to the storyboard's final view controller
window?.rootViewController = viewController
// By calling `makeKeyAndVisible()` on your window, window is shown and positioned in front of every window in your app. For the most part, you''ll only need to work with one window.
//There are instances where you'd want to create new windows to display your app's content.
// Example an external display in your app.
window?.makeKeyAndVisible()
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment