@@ -26,17 +26,17 @@ public class CBTabBarButton: UIControl {
26
26
setSelected ( newValue)
27
27
}
28
28
}
29
-
29
+
30
30
override init ( frame: CGRect ) {
31
31
super. init ( frame: frame)
32
32
configureSubviews ( )
33
33
}
34
-
34
+
35
35
required init ? ( coder aDecoder: NSCoder ) {
36
36
super. init ( coder: aDecoder)
37
37
configureSubviews ( )
38
38
}
39
-
39
+
40
40
init ( item: UITabBarItem ) {
41
41
super. init ( frame: . zero)
42
42
tabImage = UIImageView ( image: item. image)
@@ -47,24 +47,29 @@ public class CBTabBarButton: UIControl {
47
47
}
48
48
49
49
private var currentImage : UIImage ? {
50
+ var maybeImage : UIImage ?
50
51
if _isSelected {
51
- return item? . selectedImage ?? item? . image
52
+ maybeImage = item? . selectedImage ?? item? . image
52
53
} else {
53
- return item? . image
54
+ maybeImage = item? . image
54
55
}
56
+ guard let image = maybeImage else {
57
+ return nil
58
+ }
59
+ return image. renderingMode == . automatic ? image. withRenderingMode ( . alwaysTemplate) : image
55
60
}
56
-
61
+
57
62
public var item : UITabBarItem ? {
58
63
didSet {
59
- tabImage. image = currentImage? . withRenderingMode ( . alwaysTemplate )
64
+ tabImage. image = currentImage
60
65
tabLabel. text = item? . title
61
66
if let tabItem = item as? CBTabBarItem ,
62
67
let color = tabItem. tintColor {
63
68
tintColor = color
64
69
}
65
70
}
66
71
}
67
-
72
+
68
73
override public var tintColor : UIColor ! {
69
74
didSet {
70
75
if _isSelected {
@@ -78,22 +83,22 @@ public class CBTabBarButton: UIControl {
78
83
private var tabImage = UIImageView ( )
79
84
private var tabLabel = UILabel ( )
80
85
private var tabBg = UIView ( )
81
-
86
+
82
87
private let bgHeight : CGFloat = 42.0
83
88
private var csFoldedBgTrailing : NSLayoutConstraint !
84
89
private var csUnfoldedBgTrailing : NSLayoutConstraint !
85
90
private var csFoldedLblLeading : NSLayoutConstraint !
86
91
private var csUnfoldedLblLeading : NSLayoutConstraint !
87
-
92
+
88
93
private var foldedConstraints : [ NSLayoutConstraint ] {
89
94
return [ csFoldedLblLeading, csFoldedBgTrailing]
90
95
}
91
-
96
+
92
97
private var unfoldedConstraints : [ NSLayoutConstraint ] {
93
98
return [ csUnfoldedLblLeading, csUnfoldedBgTrailing]
94
99
}
95
-
96
-
100
+
101
+
97
102
private func configureSubviews( ) {
98
103
tabImage. contentMode = . center
99
104
tabImage. translatesAutoresizingMaskIntoConstraints = false
@@ -106,17 +111,17 @@ public class CBTabBarButton: UIControl {
106
111
tabImage. setContentHuggingPriority ( . required, for: . vertical)
107
112
tabImage. setContentCompressionResistancePriority ( . required, for: . horizontal)
108
113
tabImage. setContentCompressionResistancePriority ( . required, for: . vertical)
109
-
114
+
110
115
self . addSubview ( tabBg)
111
116
self . addSubview ( tabLabel)
112
117
self . addSubview ( tabImage)
113
-
118
+
114
119
tabBg. leadingAnchor. constraint ( equalTo: leadingAnchor) . isActive = true
115
120
tabBg. centerYAnchor. constraint ( equalTo: centerYAnchor) . isActive = true
116
121
tabBg. trailingAnchor. constraint ( equalTo: trailingAnchor) . isActive = true
117
122
tabBg. heightAnchor. constraint ( equalToConstant: bgHeight) . isActive = true
118
-
119
-
123
+
124
+
120
125
tabImage. leadingAnchor. constraint ( equalTo: tabBg. leadingAnchor, constant: bgHeight/ 2.0 ) . isActive = true
121
126
tabImage. centerYAnchor. constraint ( equalTo: tabBg. centerYAnchor) . isActive = true
122
127
tabLabel. centerYAnchor. constraint ( equalTo: centerYAnchor) . isActive = true
@@ -127,7 +132,7 @@ public class CBTabBarButton: UIControl {
127
132
fold ( )
128
133
setNeedsLayout ( )
129
134
}
130
-
135
+
131
136
private func fold( animationDuration duration: Double = 0.0 ) {
132
137
unfoldedConstraints. forEach { $0. isActive = false }
133
138
foldedConstraints. forEach { $0. isActive = true }
@@ -140,9 +145,9 @@ public class CBTabBarButton: UIControl {
140
145
UIView . transition ( with: tabImage, duration: duration, options: [ . transitionCrossDissolve] , animations: {
141
146
self . tabImage. tintColor = . black
142
147
} , completion: nil )
143
-
148
+
144
149
}
145
-
150
+
146
151
private func unfold( animationDuration duration: Double = 0.0 ) {
147
152
foldedConstraints. forEach { $0. isActive = false }
148
153
unfoldedConstraints. forEach { $0. isActive = true }
@@ -156,7 +161,7 @@ public class CBTabBarButton: UIControl {
156
161
self . tabImage. tintColor = self . tintColor
157
162
} , completion: nil )
158
163
}
159
-
164
+
160
165
public func setSelected( _ selected: Bool , animationDuration duration: Double = 0.0 ) {
161
166
_isSelected = selected
162
167
UIView . transition ( with: tabImage, duration: 0.05 , options: [ . beginFromCurrentState] , animations: {
@@ -168,7 +173,7 @@ public class CBTabBarButton: UIControl {
168
173
fold ( animationDuration: duration)
169
174
}
170
175
}
171
-
176
+
172
177
override public func layoutSubviews( ) {
173
178
super. layoutSubviews ( )
174
179
tabBg. layer. cornerRadius = tabBg. bounds. height / 2.0
0 commit comments