宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
//
//  UITabBarExtensition.swift
//  YYBase
//
//  Created by alvin_y on 2020/4/3.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import Foundation
 
extension UITabBar {
    /**
     添加小红点
     
     - parameter index: index
     */
    func showBadgeOnItemIndex(index : Int){
        // 移除之前的小红点
        removeBadgeOnItemIndex(index: index)
        // 新建小红点
        let badgeView = UIView()
        badgeView.tag = 888 + index
        badgeView.layer.cornerRadius = 2.5
        badgeView.backgroundColor = UIColor.red
        let tabFrame = self.frame
        // 确定小红点的位置
        let percentX = (Double(index) + 0.6) / 4
        let x = ceilf(Float(percentX) * Float(tabFrame.size.width))
        let y = ceilf(0.2 * Float(tabFrame.size.height))
        
        badgeView.frame = CGRect(x: CGFloat(x) , y: CGFloat(y), width: 5, height: 5)
        self.addSubview(badgeView)
    }
    
    func hideBadgeOnItemIndex(index : Int){
        // 移除小红点
        removeBadgeOnItemIndex(index: index)
    }
    func removeBadgeOnItemIndex(index : Int){
        // 按照tag值进行移除
        for itemView in self.subviews {
            if(itemView.tag == 888 + index){
                itemView.removeFromSuperview()
            }
        }
    }
}