
9.png
//對象
let tarCtrl = UITabBarController()
//標(biāo)簽顏色
tarCtrl.tabBar.barTintColor = UIColor.blackColor()
//字體
tarCtrl.tabBar.tintColor = UIColor.redColor()
//標(biāo)簽控制器里面裝的成員 它是以數(shù)組的形式
//把導(dǎo)航控制器加在標(biāo)簽控制器里面
let first = firstViewController()
let navCtrl = UINavigationController(rootViewController: first)
navCtrl.tabBarItem.image = UIImage(named: "78")?.imageWithRenderingMode(.AlwaysOriginal)
let second = secondViewController()
second.tabBarItem.title = "second"
second.tabBarItem.badgeValue = "10"
let third = thirdViewController()
third.tabBarItem.title = "third"
tarCtrl.viewControllers = [navCtrl,second,third]
self.window?.rootViewController = tarCtrl
協(xié)議UITabBarControllerDelegate
tarCtrl.delegate = self //代理
代理實現(xiàn)
//第二個參數(shù)為將要選中的頁面
//只能阻止通過點擊標(biāo)簽欄跳轉(zhuǎn),不能阻止通過代碼改變selectedIndex跳轉(zhuǎn)
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
let index = tabBarController.viewControllers?.indexOf(viewController)
//當(dāng)選擇第二個時,就彈出一個新的界面
if index == 1{
let four = fourViewController()
tabBarController.presentViewController(four, animated: true, completion: nil)
return false //隱藏第二頁 直接顯示four
}
return true
}
當(dāng)觸發(fā)第一個界面,就直接跳轉(zhuǎn)到第二個界面
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.tabBarController?.selectedIndex = 1
}