Pychef's Diary

I consider myself an engineer, aren't I?

SwiftでStoryboardを使わずにナビゲーションバーとタブバーを設置する

↓や「ミュージック」アプリのように画面上にNavigationBar、画面下にTabBarを置く方法です。他にも効率的なやり方があったらぜひ教えてほしいです!

f:id:pychef:20150215195554p:plain

  • AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        //2つViewControllerを用意
        var mainViewController = MainViewController()
        var secondViewController = SecondViewController()
        
        mainViewController.title = "first"
        secondViewController.title = "second"
        
        var navigationController1 = UINavigationController(rootViewController: mainViewController)
        var navigationController2 = UINavigationController(rootViewController: secondViewController)
        
        //2つのViewControllerをArrayでまとめます
        var tabs = [navigationController1, navigationController2]
        
        var tabBarController = UITabBarController()
        tabBarController.viewControllers = tabs
        
        window?.rootViewController = tabBarController
        window?.makeKeyAndVisible()
        return true
    }