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

前端之家收集整理的这篇文章主要介绍了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
}

运行后效果

原文链接:https://www.f2er.com/swift/325564.html

猜你在找的Swift相关文章