美文网首页
UITabBarController

UITabBarController

作者: galenu | 来源:发表于2019-08-08 16:20 被阅读0次
    /// TabBarItem配置信息
    fileprivate struct TabBarItemInfo {
        var vc: UIViewController
        var title: String
        var normalImgName: String
        var selectedImgName: String
    }
    
    class TabbarController: UITabBarController {
        
        fileprivate let vcInfos = [TabBarItemInfo(vc: HomeVC(),
                                                  title: "首页",
                                                  normalImgName: "main_tabbar_wifi_normal",
                                                  selectedImgName:  "main_tabbar_wifi_selected"),
                                   TabBarItemInfo(vc: MineVC(),
                                                  title: "我的",
                                                  normalImgName: "main_tabbar_speed_test_normal",
                                                  selectedImgName:  "main_tabbar_speed_test_selected"),
                                   TabBarItemInfo(vc: SettingVC(),
                                                  title: "设置",
                                                  normalImgName: "main_tabbar_setting_normal",
                                                  selectedImgName:  "main_tabbar_setting_selected")]
        
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // 设置不透明
            self.tabBar.isTranslucent = false
            
            // tabBar背景颜色
            self.tabBar.barTintColor = UIColor.white
            
            // 隐藏tabBar分割线
    //        self.tabBar.backgroundImage = UIImage()
    //        self.tabBar.shadowImage = UIImage()
            
            // 设置子控制器
            self.viewControllers = self.setupViewControllers()
        }
        
        /// 设置ViewControllers的TabBarItem
        fileprivate func setupViewControllers() -> [UIViewController] {
            var vcs = [UIViewController]()
            for info in vcInfos {
                let vc = NavigationController(rootViewController: info.vc)
                vcs.append(vc)
                vc.tabBarItem.title = info.title
                vc.tabBarItem.image = UIImage(named:  info.normalImgName)?.withRenderingMode(.alwaysOriginal)
                vc.tabBarItem.selectedImage = UIImage(named: info.selectedImgName)?.withRenderingMode(.alwaysOriginal)
                vc.tabBarItem.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
                vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10),
                                                      NSAttributedString.Key.foregroundColor: UIColor.black],
                                                     for: UIControl.State.normal)
                vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10),
                                                      NSAttributedString.Key.foregroundColor: UIColor.red],
                                                     for: UIControl.State.selected)
            }
            return vcs
        }
    }
    

    相关文章

      网友评论

          本文标题:UITabBarController

          本文链接:https://www.haomeiwen.com/subject/uigxjctx.html