Commit dfa7b8ab by Daniel Dahan Committed by GitHub

Merge pull request #1114 from OrkhanAlikhanov/tab-line-width

Added option to adjust tabBar line width
parents e8ac78ca 4bef332c
...@@ -205,6 +205,12 @@ public enum TabBarCenteringStyle { ...@@ -205,6 +205,12 @@ public enum TabBarCenteringStyle {
case always case always
} }
public enum TabBarLineStyle {
case auto
case fixed(CGFloat)
case custom((TabItem) -> CGFloat)
}
open class TabBar: Bar { open class TabBar: Bar {
/// A dictionary of TabItemLineStates to UIColors for the line. /// A dictionary of TabItemLineStates to UIColors for the line.
fileprivate var lineColorForState = [TabItemLineState: UIColor]() fileprivate var lineColorForState = [TabItemLineState: UIColor]()
...@@ -243,6 +249,13 @@ open class TabBar: Bar { ...@@ -243,6 +249,13 @@ open class TabBar: Bar {
} }
} }
/// An enum that determines the tab bar items style.
open var tabBarLineStyle = TabBarLineStyle.auto {
didSet {
layoutSubviews()
}
}
/// A reference to the scroll view when the tab bar style is scrollable. /// A reference to the scroll view when the tab bar style is scrollable.
open let scrollView = UIScrollView() open let scrollView = UIScrollView()
...@@ -486,16 +499,35 @@ fileprivate extension TabBar { ...@@ -486,16 +499,35 @@ fileprivate extension TabBar {
} }
guard shouldNotAnimateLineView else { guard shouldNotAnimateLineView else {
let f = lineFrame(for: v, forMotion: true)
line.animate(.duration(0), line.animate(.duration(0),
.size(width: v.bounds.width, height: lineHeight), .size(f.size),
.position(x: v.center.x, y: .bottom == lineAlignment ? scrollView.bounds.height - lineHeight / 2 : lineHeight / 2)) .position(f.origin))
return return
} }
line.frame = CGRect(x: v.frame.origin.x, y: .bottom == lineAlignment ? scrollView.bounds.height - lineHeight : 0, width: v.bounds.width, height: lineHeight) line.frame = lineFrame(for: v)
shouldNotAnimateLineView = false shouldNotAnimateLineView = false
} }
func lineFrame(for tabItem: TabItem, forMotion: Bool = false) -> CGRect {
let y = .bottom == lineAlignment ? scrollView.bounds.height - (forMotion ? lineHeight / 2 : lineHeight) : (forMotion ? lineHeight / 2 : 0)
let w: CGFloat = {
switch tabBarLineStyle {
case .auto:
return tabItem.bounds.width
case .fixed(let w):
return w
case .custom(let closure):
return closure(tabItem)
}
}()
let x = forMotion ? tabItem.center.x : (tabItem.frame.origin.x + (tabItem.bounds.width - w) / 2)
return CGRect(x: x, y: y, width: w, height: lineHeight)
}
} }
extension TabBar { extension TabBar {
...@@ -612,9 +644,10 @@ fileprivate extension TabBar { ...@@ -612,9 +644,10 @@ fileprivate extension TabBar {
selectedTabItem = tabItem selectedTabItem = tabItem
let f = lineFrame(for: tabItem, forMotion: true)
line.animate(.duration(0.25), line.animate(.duration(0.25),
.size(width: tabItem.bounds.width, height: lineHeight), .size(f.size),
.position(x: tabItem.center.x, y: .bottom == lineAlignment ? scrollView.bounds.height - lineHeight / 2 : lineHeight / 2), .position(f.origin),
.completion({ [weak self, isTriggeredByUserInteraction = isTriggeredByUserInteraction, tabItem = tabItem, completion = completion] in .completion({ [weak self, isTriggeredByUserInteraction = isTriggeredByUserInteraction, tabItem = tabItem, completion = completion] in
guard let `self` = self else { guard let `self` = self else {
return return
......
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