Skip to content

Instantly share code, notes, and snippets.

@tianchu
Last active July 9, 2018 22:04
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tianchu/5f899f34830c9c5d28db to your computer and use it in GitHub Desktop.
Save tianchu/5f899f34830c9c5d28db to your computer and use it in GitHub Desktop.
Create an Empty Tab-based iOS App in Swift programmatically

tl;dr; It's not always a great idea to create a tab-based app via storyboard. The following code shows you how to create an empty tab-based App in Swift without storyboard or nib.

View controllers will be created in the following structure:

                tabs
                 |
     ------------|-----------
     |           |          |
   nav1         nav2       nav3
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Override point for customization after application launch.
// Create UIWindow within the screen boundaries.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var nav1 = UINavigationController()
var first = FirstViewController(nibName: nil, bundle: nil)
nav1.viewControllers = [first]
nav1.tabBarItem = UITabBarItem(title: "Nav1", image: UIImage(named:"IconNav1"), tag: 1)
var nav2 = UINavigationController()
var second = SecondViewController(nibName: nil, bundle: nil)
nav2.viewControllers = [second]
nav2.tabBarItem = UITabBarItem(title: "Nav2", image: UIImage(named:"IconNav2"), tag: 1)
var nav3 = UINavigationController()
var third = ThirdViewController(nibName: nil, bundle: nil)
nav3.viewControllers = [third]
nav3.tabBarItem = UITabBarItem(title: "Nav3", image: UIImage(named:"IconNav3"), tag: 1)
var tabs = UITabBarController()
tabs.viewControllers = [nav1, nav2, nav3]
self.window!.rootViewController = tabs;
self.window?.makeKeyAndVisible();
return true
}
@wlaurance
Copy link

Thank you. This was helpful. I wanted to know the simplest way to get a tab view 👍

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