Commit 6cf49bce by Orkhan Alikhanov

Added tab bar centering

parent f99b6dfd
...@@ -199,6 +199,12 @@ public enum TabBarStyle: Int { ...@@ -199,6 +199,12 @@ public enum TabBarStyle: Int {
case scrollable case scrollable
} }
public enum TabBarCenteringStyle {
case never
case whenNeeded
case always
}
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]()
...@@ -229,6 +235,13 @@ open class TabBar: Bar { ...@@ -229,6 +235,13 @@ open class TabBar: Bar {
layoutSubviews() layoutSubviews()
} }
} }
/// An enum that determines the tab bar centering style.
open var tabBarCenteringStyle = TabBarCenteringStyle.never {
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()
...@@ -625,9 +638,27 @@ fileprivate extension TabBar { ...@@ -625,9 +638,27 @@ fileprivate extension TabBar {
return return
} }
if !scrollView.bounds.contains(v.frame) { let contentOffsetX: CGFloat? = {
let contentOffsetX = (v.frame.origin.x < scrollView.bounds.minX) ? v.frame.origin.x : v.frame.maxX - scrollView.bounds.width let shouldScroll = !scrollView.bounds.contains(v.frame)
let normalizedOffsetX = min(max(contentOffsetX, 0), scrollView.contentSize.width - scrollView.bounds.width)
switch tabBarCenteringStyle {
case .whenNeeded:
guard shouldScroll else {
return nil
}
fallthrough
case .always:
return v.center.x - bounds.width / 2
case .never:
guard shouldScroll else {
return nil
}
return v.frame.origin.x < scrollView.bounds.minX ? v.frame.origin.x : v.frame.maxX - scrollView.bounds.width
}
}()
if let x = contentOffsetX {
let normalizedOffsetX = min(max(x, 0), scrollView.contentSize.width - scrollView.bounds.width)
scrollView.setContentOffset(CGPoint(x: normalizedOffsetX, y: 0), animated: true) scrollView.setContentOffset(CGPoint(x: normalizedOffsetX, y: 0), animated: true)
} }
} }
......
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