微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift-UI控件开发之UITabBarController的创建

个人喜好,习惯先自定义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
}

运行后效果

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐