Swift UITabBarController


import UIKit

class TabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //禁止tabBar透明
        tabBar.isTranslucent = false
        //首页
        
        //分类
       
        addChildVC(childVC: HomeViewController(), title: "首页", norimage: "backup-media", selectedImg: "Glyphs_Inquiry_Active")
        addChildVC(childVC: MessageViewController(), title: "消息", norimage: "reminder", selectedImg: "Glyphs_Me_Active")
    }

    func addChildVC(childVC: UIViewController, title: String, norimage: String,selectedImg:String) {

        tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.red], for: .highlighted)
        var img = UIImage(named: norimage)
        img = img?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)

        var selectedImg = UIImage(named:selectedImg)
        selectedImg = selectedImg?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)

        childVC.tabBarItem.image = img
        childVC.tabBarItem.selectedImage = selectedImg
        childVC.title = title

        let nav = UINavigationController(rootViewController: childVC)
        addChild(nav)

    }
}

你可能感兴趣的:(Swift UITabBarController)