swfit 自定義Tabbar

import UIKit

class MainTabBarViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 1.初始化所有的子控制器
        setUpChildViewController()
    }
}

// MARK: - setUpChildViewController
extension MainTabBarViewController {
    fileprivate func setUpChildViewController() {
        
        let homeVC = TabBar1ViewController()
        setUpNavRootViewControllers(vc: homeVC, title: "首頁", image: "home_icon", selImage: "home_icon1")
        
        let musicVC = TabBar2ViewController()
        setUpNavRootViewControllers(vc: musicVC, title: "音樂", image: "yuershi_icon", selImage: "yuershi_icon1")
        
        let discoverVC = TabBar3ViewController()
        setUpNavRootViewControllers(vc: discoverVC, title: "發(fā)現(xiàn)", image: "kefu_icon", selImage: "kefu_icon1")
        
        let mineVC = TabBar4ViewController()
        setUpNavRootViewControllers(vc: mineVC, title: "我的", image: "wode_icon", selImage: "wode_icon1")
    }
    
    // 初始化一個到導(dǎo)航控制器的控制器
    fileprivate func setUpNavRootViewControllers(vc: UIViewController, title:String, image: String, selImage: String) {
        vc.title = title
        vc.tabBarItem.selectedImage = UIImage(named: selImage)!.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.image = UIImage(named: image)!.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes(NSDictionary(object:UIColor (colorLiteralRed: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1), forKey:NSForegroundColorAttributeName as NSCopying) as? [String : AnyObject], for:UIControlState.normal);
        vc.tabBarItem.setTitleTextAttributes(NSDictionary(object:UIColor.red, forKey:NSForegroundColorAttributeName as NSCopying) as? [String : AnyObject], for:UIControlState.selected)

//      tabbar 背景顏色
//        UITabBar.appearance().tintColor=UIColor.white
//        UITabBar.appearance().isTranslucent=false
//        這里用了一個自定義的  NavigationController
        self.addChildViewController(BaseNavigationController(rootViewController: vc))
    }
}

如有問題歡迎指正,謝謝。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容