Swift創建UITabBarController
1、創建window
// 1.創建window
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.backgroundColor = UIColor.whiteColor()
window?.rootViewController = MainViewController()//繼承與UITabBarController控制器
// 2.顯示window
window?.makeKeyAndVisible()
2、在MainViewController中
//函數重寫添加標題以及圖標
func addChildViewController(childController: UIViewController,title:String,imageName:String) {
childController.title = title;
childController.tabBarItem.image = UIImage(named: imageName)
childController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
tabBar.tintColor = UIColor.orangeColor()
let nav = UINavigationController()
nav.addChildViewController(childController)
addChildViewController(nav)
}
3、viewdidload中調用并添加標題以及圖片
addChildViewController(HomeViewController(),title:"首頁",imageName:"tabbar_home")
addChildViewController(OrderViewController(),title:"訂單",imageName: "tabbar_message_center")
addChildViewController(FreeViewController(),title: "費用",imageName: "tabbar_discover")
addChildViewController(MyViewController(), title: "我的", imageName: "tabbar_profile")