ZCTabBarController.swift 5.45 KB
//
//  XDTabBarController.swift
//  Twear
//
//  Created by yangbin on 2021/11/16.
//
import UIKit

class ZCTabBarController: UITabBarController {
    /*
    func addClick() {
        print("add")
        
        let postVC = UIStoryboard.loadViewControllerIdentifier(storyboardName: "News", identifier: "Post_Nav") as! ZCNavigationController
        postVC.modalPresentationStyle = .fullScreen
        UIViewController.getCurrentViewController()?.present(postVC, animated: true, completion: nil)

    }
     */
//    override func viewDidAppear(_ animated: Bool) {
//          super.viewDidAppear(animated)
//          
//          NotificationCenter.default.addObserver(self, selector: #selector(viewDidLoad), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
//      }
      

    override func viewDidLoad() {
        super.viewDidLoad()

        
        addChildViewControllers()
        self.selectedIndex = 0
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.black, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 11)], for: UIControl.State.selected)
        tabBar.tintColor = UIColor.black
        
        if #available(iOS 15.0, *) {
            let appearance =  UITabBar.appearance().scrollEdgeAppearance
            appearance?.configureWithOpaqueBackground()
            appearance?.backgroundColor = UIColor.systemBackground
//            UITabBar.appearance().standardAppearance = appearance!
            UITabBar.appearance().backgroundColor = UIColor.systemBackground
        }

//        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont.init(name: "PingFangSC-Regular", size: 12)!], for: .normal)
        // Do any additional setup after loading the view.
    }
    
    private func addChildViewControllers() {
        let homeVC = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Home", identifier: "nav_home")
        addChildViewController(homeVC, title: LocString("首页"), imageName: "tabbar_home", selectedImageName: "tabbar_home_selected")
        let motionVC = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Motion", identifier: "nav_motion")
        addChildViewController(motionVC, title: LocString("运动"), imageName: "tabbar_motion", selectedImageName: "tabbar_motion_selected")
        let deviceVC = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Setting", identifier: "nav_device")
        addChildViewController(deviceVC, title: LocString("设备"), imageName: "tabbar_device", selectedImageName: "tabbar_device_selected")
        let mineVC = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Mine", identifier: "nav_mine")
        addChildViewController(mineVC, title: LocString("我的"), imageName: "tabbar_mine", selectedImageName: "tabbar_mine_selected")
        
//        let tab = ZCTabBar()x
//        tab.addDelegate = self
//        self.setValue(tab, forKey: "tabBar")
    }
    
    
    
    
    func addChildViewController(_ childController: UIViewController, title: String, imageName: String, selectedImageName: String) {
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.withRenderingMode(.alwaysOriginal)
        childController.title = title
        addChild(childController)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}


/*

protocol ZCTabBarDelegate:NSObjectProtocol {
    func addClick()
}



class ZCTabBar: UITabBar {
    
    weak var addDelegate: ZCTabBarDelegate?
    

    override init(frame: CGRect) {
        super.init(frame: frame)

    }
    
    private lazy var addButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage(UIImage(named: "tabbar_add"), for: .normal)
        btn.addTarget(self, action: #selector(addButtonClick), for: .touchUpInside)
        btn.sizeToFit()
        self.addSubview(btn)
        return btn
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func addButtonClick(){
        if addDelegate != nil {
            addDelegate?.addClick()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        addButton.center = CGPoint(x: frame.size.width/2.0, y: (frame.size.height-bottomHeight)/2.0)
        
        let buttonW: CGFloat = frame.size.width / 5
        let buttonH: CGFloat = frame.size.height - bottomHeight
        let buttonY: CGFloat = 0
        
        // 按钮索引

        var index = 0
        for button in subviews {
            
            if !button.isKind(of: NSClassFromString("UITabBarButton")!){
                continue
            }
            
            let buttonX = buttonW * CGFloat(index > 1 ? (index+1) : index)
            button.frame = CGRect(x: buttonX, y: buttonY, width: buttonW, height: buttonH)
            
            index += 1
        }
    }
    
}
*/