CNLiveBaseTabBarViewController.swift 2.58 KB
//
//  CNLiveBaseTabbarViewController.swift
//  metaCode
//
//  Created by daojian on 2023/6/5.
//

import Foundation
import UIKit

class CNLiveBaseTabBarViewController:UITabBarController
{
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.setUpTabBar()
        self.setupBackgroup()
        
        self.selectedIndex = 1
        
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
    }
    // MARK: - 控制器的信息
    func setUpTabBar() {
        
        let homeVC  = CNMetaCodeHomePageController()
        let mineVC  = CNLiveMineViewController()

        creatTabbarView(viewController: homeVC, image: "tabbar_hoem_icon_default", selectImage: "tabbar_hoem_icon_selected", title: "首页")
        
        creatTabbarView(viewController: mineVC, image: "tabbar_mine_icon_default", selectImage: "tabbar_mine_icon_selected", title: "我的")

        self.tabBar.tintColor = UIColor("#0066FE")
        self.tabBar.isTranslucent = false
        
        self.viewControllers = [
            CNLiveBaseNavigationController(rootViewController: homeVC),
            CNLiveBaseNavigationController(rootViewController: mineVC),
        ];
        
        
    }
    
    // MARK: - Tabar 背景图
    func setupBackgroup(){
        if #available(iOS 13.0, *) {
            let appearance: UITabBarAppearance = UITabBarAppearance.init()
            appearance.configureWithTransparentBackground()
            appearance.backgroundColor = UIColor.clear
            appearance.backgroundImage = UIImage(named: "tabbar_backgroup")
            if #available(iOS 15.0, *) {
                tabBar.scrollEdgeAppearance = appearance
            } else {
                // Fallback on earlier versions
                tabBar.standardAppearance = appearance
            }

        } else {
            tabBar.backgroundColor = .clear
            tabBar.backgroundImage = UIImage(named: "tabbar_backgroup")
        }
    }
    
    // MARK: - TabBar里面的文字图片
    func creatTabbarView(viewController:UIViewController, image:NSString, selectImage:NSString, title:NSString) {
        // alwaysOriginal 始终绘制图片原始状态,不使用Tint Color。
        viewController.tabBarItem.image = UIImage(named: image as String)?.withRenderingMode(.alwaysOriginal)
        viewController.tabBarItem.selectedImage = UIImage(named: selectImage as String)?.withRenderingMode(.alwaysOriginal)
        viewController.title = title as String
    }
}