个人喜好,习惯先自定义UITabBarController,方便管理
1、创建UITabBarController的子类 RootTabBarController
classRootTabBarController:UITabBarController{ overridefuncviewDidLoad(){ super.viewDidLoad() }
2、在AppDelegate类里指定RootTabBarController为根视图
classAppDelegate:UIResponder,UIApplicationDelegate{ varwindow:UIWindow? funcapplication(application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:[NSObject:AnyObject]?)->Bool{ self.window=UIWindow(frame:UIScreen.mainScreen().bounds) self.window?.makeKeyAndVisible() letroot=RootTabBarController() self.window?.rootViewController=root //Overridepointforcustomizationafterapplicationlaunch. returntrue }
3、创建2个空Controller如HomeViewController、SortViewController、OtherViewController
4、在RootTabBarController类里创建tabbar的子控制器
classRootTabBarController:UITabBarController{ overridefuncviewDidLoad(){ super.viewDidLoad() //创建tabbar的子控制器 self.creatSubViewControllers() } funccreatSubViewControllers(){ letfirstVC=HomeViewController() letitem1:UITabBarItem=UITabBarItem(title:"第一页面",image:UIImage(named:"tabbar_home"),selectedImage:UIImage(named:"tabbar_home_selected")) firstVC.tabBarItem=item1 letsecondVC=SortViewController() letitem2:UITabBarItem=UITabBarItem(title:"第二页面",image:UIImage(named:"tabbar_sort"),selectedImage:UIImage(named:"tabbar_sort_selected")) secondVC.tabBarItem=item2 letotherVC=OtherViewController() letitem3:UITabBarItem=UITabBarItem(title:"第三页面",image:UIImage(named:"tabbar_other"),selectedImage:UIImage(named:"tabbar_other_selected")) otherVC.tabBarItem=item3 lettabArray=[firstVC,secondVC,otherVC] self.viewControllers=tabArray }
运行后效果