Skip to content

Instantly share code, notes, and snippets.

@j-nguyen
Last active May 28, 2021 06:59
Show Gist options
  • Save j-nguyen/11151f7c952b71ab1a3cd2b07728c4c5 to your computer and use it in GitHub Desktop.
Save j-nguyen/11151f7c952b71ab1a3cd2b07728c4c5 to your computer and use it in GitHub Desktop.
Setting up iOS Programmatically

Steps to creating iOS Project - Programmatically

Step 1 - Create the iOS Project

Create the XCode project, set-up whatever app name and organization name you want.

Step 2 - Removing Storyboard

Delete main.storyboard. Then go into info.plist and delete the key-value, Main storyboard file base name.

Step 3 - Setting up the UIWindow on AppDelegate.swift

// set up the window size
window = UIWindow(frame: UIScreen.main.bounds)
guard let window = self.window else { fatalError("no window") }

// set the view controller
window.rootViewController = ViewController()
window.makeKeyAndVisible()

This will, by default set the initial ViewController as the default screen.

Step 4 - Setting up how ViewControllers work.

Because we are not using ViewControllers anymore, we need to set-up some things. Here is how a ViewController might look.

import UIKit

class ViewController: UIViewController {

    convenience init() {
        self.init(nibName: nil, bundle: nil)
        // set up some stuff here
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        prepareView()
    }

    fileprivate func prepareView() {
        
    }
}

By default, you will need to load the aDecoder initializer, and the nibName must be set as null as they are only used from storyboards. the prepareView() is an example of how you would use it. Right now, the screen would appear as black. To see something show, set your view controller's view background colour to white.


have fun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment