@H_
502_0@个人喜好,习惯先
自定义UITabBarController,方便管理
@H_
502_0@1、创建UITabBarController的子类 RootTabBarController
classRootTabBarController:UITabBarController{
overridefuncviewDidLoad(){
super.viewDidLoad()
}
@H_
502_0@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
}
@H_
502_0@
3、创建2个空Controller如HomeViewController、SortViewController、OtherViewController
@H_
502_0@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
}
@H_
502_0@运行后
效果