Your First iOS App: 100% Programmatically (Swift)

Part 1

Three years ago, I wrote the same tutorial for Xcode 4.6. Times have changed since then.

This is Part 1 of a tutorial that follows Apple’s “Build a Basic UI”, but implements all of the user interface elements programmatically. View the full project on GitHub. View Part 2 or Part 3.

I created this tutorial for developers transitioning to iOS that don’t want to deal with Storyboards or Interface Builder. Here’s what we’re building, from Apple’s documentation:

You can select the text field and type text into it. When you click the Done button on the keyboard, the keyboard is dismissed and the label text changes to display the text in the text field. When you click the Set Default Label Text button, the label changes from what’s currently displayed in the label to Default Text.
The finished sample app.

First, open Xcode and select “Create a new Xcode project”.

Select “Single View Application”.

Choose Swift as the language, and fill out any other settings — they shouldn’t matter for this project, but you can match mine if you like.

From the file selector on the left, right click Main.storyboard and delete it.

Then, click on the project file at the top of the file selector (the blue icon that says “First”). This should display some app configuration options. Under the Deployment Info section, change the Main Interface option to be blank.

Now that we’ve removed storyboards from the project (you can look into removing the storyboard for the launch screen later), we can get to some code.

Open the AppDelegate.swift file. The App Delegate file is your application’s way of communicating with the operating system, and includes several useful methods.

We’ll mostly be dealing with “application didFinishLaunchingWithOptions”, which is called immediately after the app starts.

Add the following lines, so your method looks like this:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

let rootViewController = ViewController()
  window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = rootViewController
  return true

Notice in the left sidebar that the class ViewController.swift was already created for you. This is the class that you’re initializing in the code above. It subclasses UIViewController, which is a fundamental class in iOS development.

After, we set the window’s rootViewController property to the view controller we created. Every app must have a root view controller.

Open up the ViewController.swift file. Here you’ll find some functions Apple has predefined for you. There are several stages in what’s called the view controller lifecycle, and viewDidLoad is one of them.

In the viewDidLoad function, add another line to change your background color to white.

The code in this function should look like the block below:

override func viewDidLoad() {
self.view.backgroundColor = UIColor.whiteColor()

Build and run the app by clicking on the play button in the top left. The iOS simulator should launch with a blank white screen.

In the next part of this tutorial, we’ll handle all of the on-screen elements. Continue to Part 2.