Skip to content

Commit 1ec571b

Browse files
committed
fixed image rendering mode
1 parent e6b7f0d commit 1ec571b

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

BubbleTabBar.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22
s.name = 'BubbleTabBar'
33
s.swift_version = '4.2'
4-
s.version = '0.8.4'
4+
s.version = '0.8.5'
55
s.summary = 'One another nice animated tabbar'
66
s.homepage = 'https://github.com/Cuberto/bubble-icon-tabbar'
77
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'

BubbleTabBar/Classes/CBTabBarButton.swift

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ public class CBTabBarButton: UIControl {
2626
setSelected(newValue)
2727
}
2828
}
29-
29+
3030
override init(frame: CGRect) {
3131
super.init(frame: frame)
3232
configureSubviews()
3333
}
34-
34+
3535
required init?(coder aDecoder: NSCoder) {
3636
super.init(coder: aDecoder)
3737
configureSubviews()
3838
}
39-
39+
4040
init(item: UITabBarItem) {
4141
super.init(frame: .zero)
4242
tabImage = UIImageView(image: item.image)
@@ -47,24 +47,29 @@ public class CBTabBarButton: UIControl {
4747
}
4848

4949
private var currentImage: UIImage? {
50+
var maybeImage: UIImage?
5051
if _isSelected {
51-
return item?.selectedImage ?? item?.image
52+
maybeImage = item?.selectedImage ?? item?.image
5253
} else {
53-
return item?.image
54+
maybeImage = item?.image
5455
}
56+
guard let image = maybeImage else {
57+
return nil
58+
}
59+
return image.renderingMode == .automatic ? image.withRenderingMode(.alwaysTemplate) : image
5560
}
56-
61+
5762
public var item: UITabBarItem? {
5863
didSet {
59-
tabImage.image = currentImage?.withRenderingMode(.alwaysTemplate)
64+
tabImage.image = currentImage
6065
tabLabel.text = item?.title
6166
if let tabItem = item as? CBTabBarItem,
6267
let color = tabItem.tintColor {
6368
tintColor = color
6469
}
6570
}
6671
}
67-
72+
6873
override public var tintColor: UIColor! {
6974
didSet {
7075
if _isSelected {
@@ -78,22 +83,22 @@ public class CBTabBarButton: UIControl {
7883
private var tabImage = UIImageView()
7984
private var tabLabel = UILabel()
8085
private var tabBg = UIView()
81-
86+
8287
private let bgHeight: CGFloat = 42.0
8388
private var csFoldedBgTrailing: NSLayoutConstraint!
8489
private var csUnfoldedBgTrailing: NSLayoutConstraint!
8590
private var csFoldedLblLeading: NSLayoutConstraint!
8691
private var csUnfoldedLblLeading: NSLayoutConstraint!
87-
92+
8893
private var foldedConstraints: [NSLayoutConstraint] {
8994
return [csFoldedLblLeading, csFoldedBgTrailing]
9095
}
91-
96+
9297
private var unfoldedConstraints: [NSLayoutConstraint] {
9398
return [csUnfoldedLblLeading, csUnfoldedBgTrailing]
9499
}
95-
96-
100+
101+
97102
private func configureSubviews() {
98103
tabImage.contentMode = .center
99104
tabImage.translatesAutoresizingMaskIntoConstraints = false
@@ -106,17 +111,17 @@ public class CBTabBarButton: UIControl {
106111
tabImage.setContentHuggingPriority(.required, for: .vertical)
107112
tabImage.setContentCompressionResistancePriority(.required, for: .horizontal)
108113
tabImage.setContentCompressionResistancePriority(.required, for: .vertical)
109-
114+
110115
self.addSubview(tabBg)
111116
self.addSubview(tabLabel)
112117
self.addSubview(tabImage)
113-
118+
114119
tabBg.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
115120
tabBg.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
116121
tabBg.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
117122
tabBg.heightAnchor.constraint(equalToConstant: bgHeight).isActive = true
118-
119-
123+
124+
120125
tabImage.leadingAnchor.constraint(equalTo: tabBg.leadingAnchor, constant: bgHeight/2.0).isActive = true
121126
tabImage.centerYAnchor.constraint(equalTo: tabBg.centerYAnchor).isActive = true
122127
tabLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
@@ -127,7 +132,7 @@ public class CBTabBarButton: UIControl {
127132
fold()
128133
setNeedsLayout()
129134
}
130-
135+
131136
private func fold(animationDuration duration: Double = 0.0) {
132137
unfoldedConstraints.forEach{ $0.isActive = false }
133138
foldedConstraints.forEach{ $0.isActive = true }
@@ -140,9 +145,9 @@ public class CBTabBarButton: UIControl {
140145
UIView.transition(with: tabImage, duration: duration, options: [.transitionCrossDissolve], animations: {
141146
self.tabImage.tintColor = .black
142147
}, completion: nil)
143-
148+
144149
}
145-
150+
146151
private func unfold(animationDuration duration: Double = 0.0) {
147152
foldedConstraints.forEach{ $0.isActive = false }
148153
unfoldedConstraints.forEach{ $0.isActive = true }
@@ -156,7 +161,7 @@ public class CBTabBarButton: UIControl {
156161
self.tabImage.tintColor = self.tintColor
157162
}, completion: nil)
158163
}
159-
164+
160165
public func setSelected(_ selected: Bool, animationDuration duration: Double = 0.0) {
161166
_isSelected = selected
162167
UIView.transition(with: tabImage, duration: 0.05, options: [.beginFromCurrentState], animations: {
@@ -168,7 +173,7 @@ public class CBTabBarButton: UIControl {
168173
fold(animationDuration: duration)
169174
}
170175
}
171-
176+
172177
override public func layoutSubviews() {
173178
super.layoutSubviews()
174179
tabBg.layer.cornerRadius = tabBg.bounds.height / 2.0

BubbleTabBar/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.8.4</string>
18+
<string>0.8.5</string>
1919
<key>CFBundleVersion</key>
2020
<string>$(CURRENT_PROJECT_VERSION)</string>
2121
</dict>

0 commit comments

Comments
 (0)