Skip to content
Kevin Leong edited this page Oct 19, 2016 · 3 revisions

UITabBar

Customizing UITabBar

References

Creating a UITabBarController programatically

In AppDelegate, instantiate the custom tab bar controller:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    if let window = self.window {
        window.rootViewController = MyTabBarController()
        window.makeKeyAndVisible()
    }

    return true
}

In MyTabBarController, the following shows how to incorporate a UINavigationController with a UITabBarController programatically.

import UIKit

class MyTabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()

        setupView()
    }

    // MARK: - View Setup

    func setupView() {
        // Create separate navigation controllers for each tab
        let navigationControllerOne = createNavigationController()
        let navigationControllerTwo = createNavigationController()

        // Add both view controllers to the tab bar controller.
        viewControllers = [navigationControllerOne, navigationControllerTwo]

        // Set the title and image for each individual `UITabBarItem`
        navigationControllerOne.title = "First Screen"
        navigationControllerOne.tabBarItem.image = UIImage(named: "first-screen-icon")

        navigationControllerTwo.title = "First Screen"
        navigationControllerOne.tabBarItem.image = UIImage(named: "second-screen-icon")
    }

    # Use a storyboard generated navigation controller
    func createNavigationController() -> MovieListNavigationController {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        return storyboard
            .instantiateViewControllerWithIdentifier(
                "com.example.MyNavigationController"
            ) as! UINavigationController
    }
}

References

References

Clone this wiki locally