How To Bild An App Without Interface Builder - Part 1: Setup
Back in the days Apple shipped an empty iOS project template with Xcode. With Xcode 5 they removed it to promote the use of storyboards. When you now create an iOS project you always get a storyboard. In this post I'll show you how to remove the storyboard and how to change the AppDelegate to load a view controller using code. A similar approach can also be used to load view controllers with xibs.
Note: I'm using Xcode 7 beta 4. But what is shown here should work in Xcode 6 as well.
Open Xcode and create a new project (File/New/Project...), select the iOS/Single View Application template and click Next. Name the project (I'll use Birthdays as a working title), select Swift and check Include Unit Tests. Later we will also add UI tests. But at the beginning we want to have fast tests.
Click Next. Select a place on your hard drive to store the project and click Create.
Next you'll remove the storyboard. Select Main.storyboard and press Delete. In the alert box select Move to Trash. Open Info.plist and remove the entry with "Main storyboard file base name".
Build and run. You should see something like this:
If you ever wanted to have an app that only shows a black screen, here you go.
Now let's bring the view controller's view onto the screen. Open ViewController.swift and add the following to the end of viewDidLoad():
view.backgroundColor = .yellowColor()
With this code you set the background color of the view controller's view to yellow. I use this a lot when building UIs to see the views and to check if I have mistakes in the layout code. Build and run to make sure that the screen is still black. You expect a black screen because you haven't told iOS that it should load the view controllers view onto the screen.
Let's do exactly that. Open AppDelegate.swift and replace the line var window: UIWindow? with:
var window: UIWindow? = UIWindow(frame: UIScreen.mainScreen().bounds)
to initialize a window.
Within application(_:didFinishLaunchingWithOptions:) add the following code before the return statement:
window!.rootViewController = ViewController() window!.makeKeyAndVisible()
With this you set the rootViewController of the window and make the window the key window and tell it to show itself. Build and run. You should see this:
Congratulations! You have build your first app that doesn't use the Interface Builder.
If you enjoyed this post, then make sure you subscribe to my feed.