Blame view

Twear/Basic/ZCTabBarController.swift 4.78 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  //
  //  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 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
  //        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
          }
      }
      
  }
  */