UITabBarController

UITabBarController

创建tabBar

在appDelegate中设置tabBar相关参数
//message tab相关设置
        let messageViewController = MessageViewController()
        messageViewController.tabBarItem.title = "社区"
        messageViewController.tabBarItem.image = UIImage(named:"message")//?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        messageViewController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
        
        //main tab相关设置
        let mainViewController = MainViewController()
        mainViewController.tabBarItem.title = "主页"
        mainViewController.tabBarItem.image = UIImage(named:"main")//?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        mainViewController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
        
        //user tab相关设置
        let userViewController = UserViewController()
        userViewController.tabBarItem.title = "用户"
        userViewController.tabBarItem.image = UIImage(named:"user")//?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        userViewController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
        
        //将三个tabh加入控制器中
        let tabViewController = UITabBarController()
        tabViewController.viewControllers = [messageViewController, mainViewController, userViewController]
        

                tabViewController.selectedIndex = 1//设置加载为index0
        self.window?.rootViewController = tabViewController

tabBar的索引

@objc func enterPage() {
  self.tabBarController?.selectedIndex = 1
}

在TabBar图标上显示角标

self.tabBarItem.badgeValue = 8

你可能感兴趣的:(UITabBarController)