9.png
//对象
let tarCtrl = UITabBarController()
//标签颜色
tarCtrl.tabBar.barTintColor = UIColor.blackColor()
//字体
tarCtrl.tabBar.tintColor = UIColor.redColor()
//标签控制器里面装的成员 它是以数组的形式
//把导航控制器加在标签控制器里面
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
协议UITabBarControllerDelegate
tarCtrl.delegate = self //代理
代理实现
//第二个参数为将要选中的页面
//只能阻止通过点击标签栏跳转,不能阻止通过代码改变selectedIndex跳转
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
let index = tabBarController.viewControllers?.indexOf(viewController)
//当选择第二个时,就弹出一个新的界面
if index == 1{
let four = fourViewController()
tabBarController.presentViewController(four, animated: true, completion: nil)
return false //隐藏第二页 直接显示four
}
return true
}
当触发第一个界面,就直接跳转到第二个界面
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.tabBarController?.selectedIndex = 1
}