【swift】UITabBarControllerひな型
これも毎回過去のコードをみてるのでひな型をメモしておきます。
UITabBarController配下にUINavigtionControllerを配置する形です。
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate { ・・・ func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let tabbarController: TabbarController = TabbarController() self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window?.rootViewController = tabbarController self.window?.makeKeyAndVisible() return true } }
TabbarController.swift
class TabbarController: UITabBarController { var navController: UINavigationController! var navController2: UINavigationController! var navController3: UINavigationController! override func viewDidLoad() { super.viewDidLoad() // ViewController作成 let vc = UIViewController() vc.view.backgroundColor = UIColor.whiteColor() vc.navigationItem.title = "ナビ1" navController = UINavigationController(rootViewController: vc) let vc2 = UIViewController() vc2.view.backgroundColor = UIColor.whiteColor() vc2.navigationItem.title = "ナビ2" navController2 = UINavigationController(rootViewController: vc2) let vc3 = UIViewController() vc3.view.backgroundColor = UIColor.whiteColor() vc3.navigationItem.title = "ナビ3" navController3 = UINavigationController(rootViewController: vc3) // タブバーの色設定 UITabBar.appearance().tintColor = UIColor.blackColor() UITabBar.appearance().barTintColor = UIColor.whiteColor() // タブ画像設定 navController.tabBarItem.image = UIImage(named: "tab.png") navController2.tabBarItem.image = UIImage(named: "tab2.png") navController3.tabBarItem.image = UIImage(named: "tab3.png") // タブタイトル設定 navController.tabBarItem.title = "タブ1" navController2.tabBarItem.title = "タブ2" navController3.tabBarItem.title = "タブ3" let tabs = NSArray(objects: navController, navController2, navController3) self.setViewControllers(tabs as! [UINavigationController], animated: true) } }
ここではViewControllerを直接書いてますが、別ファイルにするのが一般的ですね
以上です