杨锴
2024-08-14 909e20941e45f8712c012db602034b47da0bfdb0
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
// UITabBarExtensions.swift - Copyright 2024 SwifterSwift
 
#if canImport(UIKit) && !os(watchOS)
import UIKit
 
// MARK: - Methods
 
public extension UITabBar {
    /// SwifterSwift: Set tabBar colors.
    ///
    /// - Parameters:
    ///   - background: background color.
    ///   - selectedBackground: background color for selected tab.
    ///   - item: icon tint color for items.
    ///   - selectedItem: icon tint color for item.
    func setColors(
        background: UIColor? = nil,
        selectedBackground: UIColor? = nil,
        item: UIColor? = nil,
        selectedItem: UIColor? = nil) {
        // background
        barTintColor = background ?? barTintColor
 
        // selectedItem
        tintColor = selectedItem ?? tintColor
        // shadowImage = UIImage()
        backgroundImage = UIImage()
        #if !os(visionOS)
        isTranslucent = false
        #endif
 
        // selectedBackgroundColor
        guard let barItems = items else {
            return
        }
 
        if let selectedbg = selectedBackground {
            let rect = CGSize(width: frame.width / CGFloat(barItems.count), height: frame.height)
            selectionIndicatorImage = { (color: UIColor, size: CGSize) -> UIImage in
                return UIGraphicsImageRenderer(size: size).image { context in
                    color.setFill()
                    context.fill(context.format.bounds)
                }
            }(selectedbg, rect)
        }
 
        if let itemColor = item {
            for barItem in barItems as [UITabBarItem] {
                // item
                guard let image = barItem.image else { continue }
 
                barItem.image = { (image: UIImage, color: UIColor) -> UIImage in
                    guard let mask = image.cgImage else { return image }
 
                    let format = UIGraphicsImageRendererFormat()
                    format.scale = image.scale
                    return UIGraphicsImageRenderer(size: image.size, format: format).image {
                        let context = $0.cgContext
 
                        color.setFill()
 
                        context.translateBy(x: 0, y: image.size.height)
                        context.scaleBy(x: 1.0, y: -1.0)
                        context.setBlendMode(CGBlendMode.normal)
 
                        let rect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
                        context.clip(to: rect, mask: mask)
                        context.fill(rect)
                    }
                }(image, itemColor).withRenderingMode(.alwaysOriginal)
 
                barItem.setTitleTextAttributes([.foregroundColor: itemColor], for: .normal)
                if let selected = selectedItem {
                    barItem.setTitleTextAttributes([.foregroundColor: selected], for: .selected)
                }
            }
        }
    }
}
 
#endif