Swift学习笔记(二):使用UITabBarController

563 阅读1分钟

使用UITabBarController

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {

        super.viewDidLoad()

        initTabBar()

    }

    
    func initTabBar() {

        let home = HomeViewController()

        home.tabBarItem.title = "首页"

        home.tabBarItem.image = UIImage(named: "tab_home_normal")

        home.tabBarItem.selectedImage = UIImage(named: "tab_home_selected")

        

        let category = CategoryViewController()

        category.tabBarItem.title = "分类"

        category.tabBarItem.image = UIImage(named: "tab_category_normal")

        category.tabBarItem.selectedImage = UIImage(named: "tab_category_selected")

        

        let found = FoundViewController()

        found.tabBarItem.title = "发现"

        found.tabBarItem.image = UIImage(named: "tab_found_normal")

        found.tabBarItem.selectedImage = UIImage(named: "tab_found_selected")

        

        let cart = CartViewController()

        cart.tabBarItem.title = "购物车"

        cart.tabBarItem.image = UIImage(named: "tab_cart_normal")

        cart.tabBarItem.selectedImage = UIImage(named: "tab_cart_selected")

        

        let mine = MineViewController()

        mine.tabBarItem.title = "我的"

        mine.tabBarItem.image = UIImage(named: "tab_mine_normal")

        mine.tabBarItem.selectedImage = UIImage(named: "tab_minee_selected")

        

        let homeNavi = BaseNavigationController(rootViewController: home)

        let categoryNavi = BaseNavigationController(rootViewController: category)

        let foundNavi = BaseNavigationController(rootViewController: found)

        let cartNavi = BaseNavigationController(rootViewController: cart)

        let mineNavi = BaseNavigationController(rootViewController: mine)

        viewControllers = [homeNavi, categoryNavi, foundNavi, cartNavi, mineNavi]

        

        // 设置 tabBar & tabBarItem

        setTabBarItemAttributes(bgColor: UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1))

        

    }

    

    func setTabBarItemAttributes(fontName: String = "Courier",

                                     fontSize: CGFloat = 12,

                                     normalColor: UIColor = UIColor(colorHex: 0x666666),

                                     selectedColor: UIColor = UIColor(colorHex: 0xd81e06),

                                     bgColor: UIColor = .white) {

        // tabBarItem 文字大小

        var attributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: fontName, size: fontSize)!]

        

        // tabBarItem 文字默认颜色

        attributes[.foregroundColor] = normalColor

        UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)

        

        // tabBarItem 文字选中颜色

        attributes[.foregroundColor] = selectedColor

        UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .selected)

        

        // tabBar 文字、图片 统一选中高亮色

        tabBar.tintColor = selectedColor

        

        // tabBar 背景色

        tabBar.barTintColor = bgColor

        tabBar.backgroundColor = bgColor


    }


}