Commit 03a4e9f8 by Daniel Dahan

issue-860: Updated TabBar color states and added an independent line color state.

parent 2bd5e608
## 2.12.2
* [issue-860](https://github.com/CosmicMind/Material/issues/860): Updated TabBar color states and added an independent line color state.
## 2.12.1 ## 2.12.1
* [issue-911](https://github.com/CosmicMind/Material/issues/911): Added @objc to TabBar.tabItems for visibility in Obj-C. * [issue-911](https://github.com/CosmicMind/Material/issues/911): Added @objc to TabBar.tabItems for visibility in Obj-C.
......
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = 'Material' s.name = 'Material'
s.version = '2.12.1' s.version = '2.12.2'
s.license = 'BSD-3-Clause' s.license = 'BSD-3-Clause'
s.summary = 'A UI/UX framework for creating beautiful applications.' s.summary = 'A UI/UX framework for creating beautiful applications.'
s.homepage = 'http://materialswift.com' s.homepage = 'http://materialswift.com'
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>FMWK</string> <string>FMWK</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>2.12.1</string> <string>2.12.2</string>
<key>CFBundleSignature</key> <key>CFBundleSignature</key>
<string>????</string> <string>????</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
......
...@@ -35,11 +35,29 @@ open class TabItem: FlatButton { ...@@ -35,11 +35,29 @@ open class TabItem: FlatButton {
super.prepare() super.prepare()
pulseAnimation = .none pulseAnimation = .none
} }
open override var isHighlighted: Bool {
didSet {
tintColor = titleColor(for: isHighlighted ? .highlighted : isSelected ? .selected : .normal)
}
}
open override var isSelected: Bool {
didSet {
tintColor = titleColor(for: isSelected ? .selected : .normal)
}
}
} }
@objc(TabItemState) @objc(TabItemState)
public enum TabItemState: Int { public enum TabItemState: Int {
case normal case normal
case highlighted
case selected
}
@objc(TabItemLineState)
public enum TabItemLineState: Int {
case selected case selected
} }
...@@ -120,9 +138,12 @@ open class TabBar: Bar { ...@@ -120,9 +138,12 @@ open class TabBar: Bar {
return w return w
} }
/// A dictionary of TabItemStates to UIColors. /// A dictionary of TabItemStates to UIColors for tabItems.
fileprivate var tabItemsColorForState = [TabItemState: UIColor]() fileprivate var tabItemsColorForState = [TabItemState: UIColor]()
/// A dictionary of TabItemLineStates to UIColors for the line.
fileprivate var lineColorForState = [TabItemLineState: UIColor]()
/// An enum that determines the tab bar style. /// An enum that determines the tab bar style.
open var tabBarStyle = TabBarStyle.auto { open var tabBarStyle = TabBarStyle.auto {
didSet { didSet {
...@@ -152,6 +173,7 @@ open class TabBar: Bar { ...@@ -152,6 +173,7 @@ open class TabBar: Bar {
willSet { willSet {
selectedTabItem?.isSelected = false selectedTabItem?.isSelected = false
} }
didSet { didSet {
selectedTabItem?.isSelected = true selectedTabItem?.isSelected = true
} }
...@@ -214,17 +236,7 @@ open class TabBar: Bar { ...@@ -214,17 +236,7 @@ open class TabBar: Bar {
} }
/// A reference to the line UIView. /// A reference to the line UIView.
open let line = UIView() fileprivate let line = UIView()
/// The line color.
open var lineColor: UIColor? {
get {
return line.backgroundColor
}
set(value) {
line.backgroundColor = value
}
}
/// A value for the line alignment. /// A value for the line alignment.
open var lineAlignment = TabBarLineAlignment.bottom { open var lineAlignment = TabBarLineAlignment.bottom {
...@@ -265,9 +277,11 @@ open class TabBar: Bar { ...@@ -265,9 +277,11 @@ open class TabBar: Bar {
prepareScrollView() prepareScrollView()
prepareDivider() prepareDivider()
prepareLine() prepareLine()
prepareColors() prepareTabItemsColor()
prepareLineColor()
updateColors() updateTabItemColors()
updateLineColors()
} }
} }
...@@ -300,9 +314,15 @@ fileprivate extension TabBar { ...@@ -300,9 +314,15 @@ fileprivate extension TabBar {
} }
/// Prepares the tabsItems colors. /// Prepares the tabsItems colors.
func prepareColors() { func prepareTabItemsColor() {
tabItemsColorForState[.normal] = Color.blue.base tabItemsColorForState[.normal] = Color.grey.base
tabItemsColorForState[.selected] = Color.blue.base tabItemsColorForState[.selected] = Color.blue.base
tabItemsColorForState[.highlighted] = Color.blue.base
}
/// Prepares the line colors.
func prepareLineColor() {
lineColorForState[.selected] = Color.blue.base
} }
/** /**
...@@ -311,6 +331,7 @@ fileprivate extension TabBar { ...@@ -311,6 +331,7 @@ fileprivate extension TabBar {
*/ */
func prepareTabItemHandler(tabItem: TabItem) { func prepareTabItemHandler(tabItem: TabItem) {
removeTabItemHandler(tabItem: tabItem) removeTabItemHandler(tabItem: tabItem)
tabItem.addTarget(self, action: #selector(handleTabItemsChange(tabItem:)), for: .touchUpInside) tabItem.addTarget(self, action: #selector(handleTabItemsChange(tabItem:)), for: .touchUpInside)
tabItem.addTarget(self, action: #selector(handleLineAnimation(tabItem:)), for: .touchUpInside) tabItem.addTarget(self, action: #selector(handleLineAnimation(tabItem:)), for: .touchUpInside)
} }
...@@ -400,7 +421,6 @@ fileprivate extension TabBar { ...@@ -400,7 +421,6 @@ fileprivate extension TabBar {
@objc @objc
func handleTabItemsChange(tabItem: TabItem) { func handleTabItemsChange(tabItem: TabItem) {
selectedTabItem = tabItem selectedTabItem = tabItem
updateColors()
} }
/// Handles the tabItem touch event. /// Handles the tabItem touch event.
...@@ -440,35 +460,43 @@ extension TabBar { ...@@ -440,35 +460,43 @@ extension TabBar {
extension TabBar { extension TabBar {
/** /**
Sets the colors of the tabItems for the state given. Sets the color for the tabItems given a TabItemState.
- Parameter _ color: A UIColor. - Parameter _ color: A UIColor.
- Parameter for state: A TabItemState. - Parameter for state: A TabItemState.
*/ */
open func setColor(_ color: UIColor, for state: TabItemState) { open func setTabItemsColor(_ color: UIColor, for state: TabItemState) {
tabItemsColorForState[state] = color tabItemsColorForState[state] = color
updateColors() updateTabItemColors()
}
/**
Sets the color for the line given a TabItemLineState.
- Parameter _ color: A UIColor.
- Parameter for state: A TabItemLineState.
*/
open func setLineColor(_ color: UIColor, for state: TabItemLineState) {
lineColorForState[state] = color
updateLineColors()
} }
} }
fileprivate extension TabBar { fileprivate extension TabBar {
/// Updates the tabItems colors. /// Updates the tabItems colors.
func updateColors() { func updateTabItemColors() {
var color = tabItemsColorForState[.normal] let normalColor = tabItemsColorForState[.normal]
let selectedColor = tabItemsColorForState[.selected]
let highlightedColor = tabItemsColorForState[.highlighted]
for v in tabItems { for v in tabItems {
v.setTitleColor(color, for: .normal) v.setTitleColor(normalColor, for: .normal)
v.tintColor = color v.setTitleColor(selectedColor, for: .selected)
v.setTitleColor(highlightedColor, for: .highlighted)
} }
guard let v = selectedTabItem else {
return
} }
color = tabItemsColorForState[.selected] /// Updates the line colors.
v.setTitleColor(color, for: .selected) func updateLineColors() {
v.tintColor = color line.backgroundColor = lineColorForState[.selected]
lineColor = color
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment