Storyboard Navigation vs. Programmatic Navigation in Swift

Which should you use and why?

Limits and Cons of Xcode Storyboards

Should I switch to Swift-driven navigation?

When your iOS app becomes a challenge, Xcode Storyboards doesn’t seem to be the right choice. When you’re implementing controllers and views, the Xcode Storyboard starts to feel slow to load and unclear, and results begin to look like a massive collection of puzzling arrows between connecting boxes. Thanks to the Cocoa framework and the UIKit framework, you can choose not to use the Storyboard and conceive a navigation pattern through coding, by pushing controllers and presenting modals exclusively coding in Swift (or Objective-C, if you’re a nostalgic programmer).

Limits and Cons of Xcode Storyboards

The Xcode storyboard is the natural evolution of the old Interface Builder, a deprecated app that developers used to create views, IBOutlets and IBActions (which keep the IB prefix even today). The old Interface Builder was an app itself, totally standalone, and has been discontinued since the release of Xcode 5. Then, new .storyboards files were introduced in Xcode and they were a breakthrough —a useful pattern to conceive navigation patterns in modern iOS app development.

Storyboards are a great tool as they give developers a good representation of how the app works in its entirety. More and more use auto-layout features that are able to transform a view in some kind of «responsive» views, ready to be adaptive on small and large displays of todays Apple devices. But after all..

I found Storyboards hard to be read and useful when you’re app keep growing and I’m stuck with slow loading times. Soon, you will absolutely lose control of your storyboard and it just become a mess.

Should I switch to Swift-driven navigation?

First: definitively yes if your app is made of dozens of controllers. I found Swift-driven navigation better when you have many connections between controllers. Sometimes code is even more readable than a storyboard to me. By using Storyboards, you can declare connection between controllers in a friendly and smart way, but we like Swift and opening modals or pushing controllers can be done in just one or two lines of code. Moreover, it’s a very common pattern to pass data to controllers you’re pushing, so you can’t really make an app navigate without coding. Yes, the “prepare for segue” methods can do the trick, but they look annoying when you have complex logic and you have to switch-case the controller you’re about to land on.

So, how do I stop using Storyboard and use Swift navigation?

First, set your target without a “Main Interface” by clicking on the project title and by simply deleting the Storyboard reference.

Don’t forget to set a UIViewController as a rootViewController as soon as your app becomes active, here we’re starting with a basic splash UIViewController:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {        self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = Splash()
self.window?.makeKeyAndVisible()
self.window?.backgroundColor = .white
}

Then, just start coding anytime you need to present a new ViewController.
I’ll show you some code examples that will make it clear:

// present a modal with an embed UINavigationController
let rvc = MyNewController()
let vc = UINavigationController(rootViewController: rvc)
vc.modalPresentationStyle = .overFullScreen
present(vc, animated: true, completion: nil)

Easy, you don’t even need to instatiate a UINib or refer a bundle. It’s the same for pushing a ViewController on you current UINavigationController:

let vc = MyPushedNewController()
vc.param = value
navigationController?.pushViewController(vc, animated: true)

Final considerations

  1. Keep using Storyboards if your app is simple and you don’t need complex navigation patterns or to pass params to the next controller.
  2. Storyboards could lead to faster implementations, but again, only if your app is not complex.
  3. Switch to Swift-driven navigation if you like coding. You can always use a custom class to wrap navigations or build ViewControllers to push.

Enjoy your iOS app, small or large!