123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- //
- // BaseTabbarController.swift
- // ADHTuanCan
- //
- // Created by 敖德亨 on 2023/9/30.
- //
- import UIKit
- import SwiftyUserDefaults
- class BaseTabbarController: UITabBarController {
-
- override func viewDidLoad() {
- super.viewDidLoad()
- setupChildControllers()
- tabBar.isTranslucent = false
- }
-
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
-
- }
- //extension 类似于OC中的分类,在Swift中还可以用来切分代码块
- //可以把相近功能的函数,放在一个extension中
- //注意:和OC的分类一样,extension中不能定义属性
- //MARK: -设置界面
- extension BaseTabbarController {
-
- /// 设置所有子控制器
- func setupChildControllers(){
- var array = [["clsName":"HomeViewController","title":LanguagesUtil.createTextBy(Ctext: "首页", Etext: "Home"),"imageName":"首页"],["clsName":"HomeSecondViewController","title":LanguagesUtil.createTextBy(Ctext: "普团", Etext: "General Home"),"imageName":"普团"],["clsName":"OrderManagerVC","title":LanguagesUtil.createTextBy(Ctext: "订单", Etext: "Order"),"imageName":"订单"],["clsName":"MyViewController","title":LanguagesUtil.createTextBy(Ctext: "我的", Etext: "My"),"imageName":"我的"]
- ]
-
- if Defaults[\.userType] === 1{
- array = [["clsName":"HomeSecondViewController","title":LanguagesUtil.createTextBy(Ctext: "普团", Etext: "General Home"),"imageName":"普团"],
- ["clsName":"OrderManagerVC","title":LanguagesUtil.createTextBy(Ctext: "订单", Etext: "Order"),"imageName":"订单"],
- ["clsName":"MyViewController","title":LanguagesUtil.createTextBy(Ctext: "我的", Etext: "My"),"imageName":"我的"]
- ]
- }
-
- // let array = vcList
- var arrayM = [UIViewController]()
-
- for dict in array {
-
- arrayM.append(controller(dict: dict))
- }
- ///Use of unresolved identifier 'viewControllers'
- viewControllers = arrayM
-
- //tabbar选中背景图重新调整大小
- var imageName = "tabbar_selectedBackImage"
-
- tabBar.selectionIndicatorImage = tabBarSelecedBackImage(imageName: imageName, imageSize: CGSize(width: kSCREEN_WIDTH/CGFloat((viewControllers?.count)!), height: kTabBarHeight))
-
- tabBar.barTintColor = UIColor.white
-
- }
-
- /// 使用字典创建一个子控制器
- ///
- /// - Parameter dict: 信息字典
- /// - Returns: 子视图控制器
- private func controller(dict: [String: String])->UIViewController{
-
- //1,取得字典内容
- //guard语句判断其后的表达式布尔值为false时,才会执行之后代码块里的代码,如果为true,则跳过整个guard语句
- var namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String
- guard
- let clsName = dict["clsName"],
- let title = dict["title"],
- let imageName = dict["imageName"],
- //命名空间 项目的名字 + "." + "类名"
- let anyClass = NSClassFromString(namespace! + "." + clsName) as? UIViewController.Type
- else{
- return UIViewController()
- }
-
-
- //2.创建视图控制器
- let vc = anyClass.init()
-
- vc.title = title
- //3.设置图像
- vc.tabBarItem.image = UIImage(named:imageName + "未选中")?.withRenderingMode(.alwaysOriginal)
- vc.tabBarItem.selectedImage = UIImage(named: imageName + "选中")?.withRenderingMode(.alwaysOriginal)
- //4.设置tabBar的标题字体(大小)
- // vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: UIControl.State.normal)
- vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : kThemeColor], for: UIControl.State.highlighted)
- //系统默认是12号字,修改字体大小,要设置Normal的字体大小
- //vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12)], for: .normal)
- //5.设置tabbarItem选中背景图
- //实例化导航控制器的时候,会调用重载的push方法 将rootVC进行压栈
- let nav = ADHBaseNavigationController(rootViewController: vc)
-
- return nav
-
- }
- func tabBarSelecedBackImage(imageName:String,imageSize:CGSize) -> UIImage {
- let originalImage = UIImage(named: imageName)
- let rect : CGRect = CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height)
- UIGraphicsBeginImageContext(rect.size)
- originalImage?.draw(in: rect)
- let image : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
-
- UIGraphicsEndImageContext()
-
- return image
- }
- }
|