1 创建UITabBarController的子类 RootTabBarController
class RootTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
2、在AppDelegate类里指定RootTabBarController为根视图
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.makeKeyAndVisible()
let root = RootTabBarController()
self.window?.rootViewController=root
// Override point for customization after application launch.
return true
}
3、创建2个空Controller如HomeViewController、SortViewController、OtherViewController
注: 这三个Controller是导航标签的跳转页,每一个都是一个继承自UIViewController的 Controller,在这里我们的跳转页并不做什么,所以这里的Controller没有任何实现
import UIKit
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
4、在RootTabBarController类里创建tabbar的子控制器
class RootTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
//创建tabbar的子控制器
self.creatSubViewControllers()
}
func creatSubViewControllers(){
let firstVC = HomeViewController ()
let item1 : UITabBarItem = UITabBarItem (title: "中国", image: UIImage(named: "图标名"), selectedImage: UIImage(named: "tabbar_home_selected"))
firstVC.tabBarItem = item1
let secondVC = SortViewController ()
let item2 : UITabBarItem = UITabBarItem (title: "日本", image: UIImage(named: "图标名"), selectedImage: UIImage(named: "tabbar_sort_selected"))
secondVC.tabBarItem = item2
let otherVC = OtherViewController ()
let item3 : UITabBarItem = UITabBarItem (title: "韩国", image: UIImage(named: "图标名"), selectedImage: UIImage(named: "tabbar_other_selected"))
otherVC.tabBarItem = item3
let tabArray = [firstVC,secondVC,otherVC]
// 背景色可通过这样设置
self.tabBar.translucent = false //避免受默认的半透明色影响,关闭
//self.tabBar.tintColor = UIColor.yellowColor() //设置选中颜色,这里使用黄色
//self.tabBar.tintColor =UIColor(red: 0/255, green:188/255, blue:212/255, alpha:1) //也可以自定义颜色
self.viewControllers = tabArray
}
运行后效果: