BaseTabbarController.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // BaseTabbarController.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/9/30.
  6. //
  7. import UIKit
  8. import SwiftyUserDefaults
  9. class BaseTabbarController: UITabBarController {
  10. override func viewDidLoad() {
  11. super.viewDidLoad()
  12. setupChildControllers()
  13. tabBar.isTranslucent = false
  14. }
  15. override func didReceiveMemoryWarning() {
  16. super.didReceiveMemoryWarning()
  17. // Dispose of any resources that can be recreated.
  18. }
  19. }
  20. //extension 类似于OC中的分类,在Swift中还可以用来切分代码块
  21. //可以把相近功能的函数,放在一个extension中
  22. //注意:和OC的分类一样,extension中不能定义属性
  23. //MARK: -设置界面
  24. extension BaseTabbarController {
  25. /// 设置所有子控制器
  26. func setupChildControllers(){
  27. 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":"我的"]
  28. ]
  29. if Defaults[\.userType] === 1{
  30. array = [["clsName":"HomeSecondViewController","title":LanguagesUtil.createTextBy(Ctext: "普团", Etext: "General Home"),"imageName":"普团"],
  31. ["clsName":"OrderManagerVC","title":LanguagesUtil.createTextBy(Ctext: "订单", Etext: "Order"),"imageName":"订单"],
  32. ["clsName":"MyViewController","title":LanguagesUtil.createTextBy(Ctext: "我的", Etext: "My"),"imageName":"我的"]
  33. ]
  34. }
  35. // let array = vcList
  36. var arrayM = [UIViewController]()
  37. for dict in array {
  38. arrayM.append(controller(dict: dict))
  39. }
  40. ///Use of unresolved identifier 'viewControllers'
  41. viewControllers = arrayM
  42. //tabbar选中背景图重新调整大小
  43. var imageName = "tabbar_selectedBackImage"
  44. tabBar.selectionIndicatorImage = tabBarSelecedBackImage(imageName: imageName, imageSize: CGSize(width: kSCREEN_WIDTH/CGFloat((viewControllers?.count)!), height: kTabBarHeight))
  45. tabBar.barTintColor = UIColor.white
  46. }
  47. /// 使用字典创建一个子控制器
  48. ///
  49. /// - Parameter dict: 信息字典
  50. /// - Returns: 子视图控制器
  51. private func controller(dict: [String: String])->UIViewController{
  52. //1,取得字典内容
  53. //guard语句判断其后的表达式布尔值为false时,才会执行之后代码块里的代码,如果为true,则跳过整个guard语句
  54. var namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String
  55. guard
  56. let clsName = dict["clsName"],
  57. let title = dict["title"],
  58. let imageName = dict["imageName"],
  59. //命名空间 项目的名字 + "." + "类名"
  60. let anyClass = NSClassFromString(namespace! + "." + clsName) as? UIViewController.Type
  61. else{
  62. return UIViewController()
  63. }
  64. //2.创建视图控制器
  65. let vc = anyClass.init()
  66. vc.title = title
  67. //3.设置图像
  68. vc.tabBarItem.image = UIImage(named:imageName + "未选中")?.withRenderingMode(.alwaysOriginal)
  69. vc.tabBarItem.selectedImage = UIImage(named: imageName + "选中")?.withRenderingMode(.alwaysOriginal)
  70. //4.设置tabBar的标题字体(大小)
  71. // vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: UIControl.State.normal)
  72. vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : kThemeColor], for: UIControl.State.highlighted)
  73. //系统默认是12号字,修改字体大小,要设置Normal的字体大小
  74. //vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12)], for: .normal)
  75. //5.设置tabbarItem选中背景图
  76. //实例化导航控制器的时候,会调用重载的push方法 将rootVC进行压栈
  77. let nav = ADHBaseNavigationController(rootViewController: vc)
  78. return nav
  79. }
  80. func tabBarSelecedBackImage(imageName:String,imageSize:CGSize) -> UIImage {
  81. let originalImage = UIImage(named: imageName)
  82. let rect : CGRect = CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height)
  83. UIGraphicsBeginImageContext(rect.size)
  84. originalImage?.draw(in: rect)
  85. let image : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
  86. UIGraphicsEndImageContext()
  87. return image
  88. }
  89. }