iOS App Programmatically: Working without the storyboard

You want to start a new iOS project without using the storyboard or nib ? This tutorial is for you.

In this tutorial, we will:

  1. Create new project
  2. Remove the storyboard
  3. Setup the AppDelegate.swift

For this tutorial, we use:

  1. Xcode 8.2
  2. Swift 3

Next tutorial:

Create new project

The first step, select “create a new Xcode project
iOS tuto1 - 1

Then select “Single View Application
iOS tuto1 - 2

Next, fill the textfields with your informations.

  •  Product Name: the name of your application
  • Organization Name: the name of your organization
  • Organization Identifier: generally, you inverse your website url name
  • Language: we will use “swift”
  • Devices: select iPhone or iPad or Universal (for iPhone and iPad)
  • In this tutorial, we will not use Core Data (iOS database)

iOS tuto1 - 3

Finally, save the project to your computer. If you use Git, check “Create Git repository” or you can also do it manually later.
iOS tuto1 - 4

The project navigator should look like this
iOS tuto1 - 5

Remove the storyboard

After creating the project, we will remove the storyboard from the project. So in the project navigator, delete “Main.storyboard” 

Then select “Move to Trash” on the alert window
iOS tuto1 - 6
Next, select “info.plist” in the project navigator and you will find “Main storyboard file base name”. Delete it.
iOS tuto1 - 7

We have now remove the storyboard from the project. You can launch the application however the application will be empty (black screen).

Set up the AppDelegate

The AppDelegate is the root object of your application. It has several important roles.

  • It contains your app’s startup code.
  • It responds to key changes in the state of your app.
  • More information here.

iOS tuto1 - 8

In the “didFinishLaunchingWithOptions” add the following line:

self.window = UIWindow(frame: UIScreen.main.bounds)

It will initialise the window with the size of the screen device.

guard let window = self.window else { fatalError(“no window”) }

A guard let to be sure that the window is not nil.

window.rootViewController = ViewController()

Set the root view controller of the app’s window.
ViewController() : instance of the ‘ViewController’ class which is locate in the project navigator. For less confusion, you can rename the class and the file name by MainViewController or MainVC.

window.makeKeyAndVisible()

Make the window visible.

The AppDelegate should be similar to this:
iOS tuto1 - 12

You can download the tutorial project:
https://github.com/ndtran/ToMyLab/tree/master/Tutorial1/

Next step

Now you have setup your project and you can start to modify the view programmatically. The next step will be to add some elements as UILabel, UIButton or UITextField, to the user interface.

Next tutorial: iOS App programmatically: UILabel, UITextField and UIButton

iOS App Programmatically: Working without the storyboard