Commit d75ed300 by Daniel Dahan

initial scrollable TabBar work added

parent 0c526c8f
...@@ -105,7 +105,7 @@ open class Bar: View { ...@@ -105,7 +105,7 @@ open class Bar: View {
} }
/// ContentView that holds the any desired subviews. /// ContentView that holds the any desired subviews.
open let contentView = UIView() open let contentView = UIScrollView()
/// Left side UIViews. /// Left side UIViews.
open var leftViews: [UIView] { open var leftViews: [UIView] {
...@@ -272,5 +272,17 @@ open class Bar: View { ...@@ -272,5 +272,17 @@ open class Bar: View {
autoresizingMask = .flexibleWidth autoresizingMask = .flexibleWidth
interimSpacePreset = .interimSpace3 interimSpacePreset = .interimSpace3
contentEdgeInsetsPreset = .square1 contentEdgeInsetsPreset = .square1
prepareContentView()
}
}
extension Bar {
/// Prepares the contentView.
fileprivate func prepareContentView() {
contentView.bounces = false
contentView.isPagingEnabled = true
contentView.showsVerticalScrollIndicator = false
contentView.showsHorizontalScrollIndicator = false
contentView.contentScaleFactor = Screen.scale
} }
} }
...@@ -196,6 +196,7 @@ open class PageMenuController: UIViewController { ...@@ -196,6 +196,7 @@ open class PageMenuController: UIViewController {
when subclassing. when subclassing.
*/ */
open func prepare() { open func prepare() {
view.contentScaleFactor = Screen.scale
prepareScrollView() prepareScrollView()
prepareViewControllers() prepareViewControllers()
} }
...@@ -209,6 +210,7 @@ extension PageMenuController { ...@@ -209,6 +210,7 @@ extension PageMenuController {
scrollView.isPagingEnabled = true scrollView.isPagingEnabled = true
scrollView.showsVerticalScrollIndicator = false scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false scrollView.showsHorizontalScrollIndicator = false
scrollView.contentScaleFactor = Screen.scale
view.addSubview(scrollView) view.addSubview(scrollView)
} }
...@@ -287,6 +289,7 @@ extension PageMenuController { ...@@ -287,6 +289,7 @@ extension PageMenuController {
tabBar?.isLineAnimated = false tabBar?.isLineAnimated = false
tabBar?.lineAlignment = .top tabBar?.lineAlignment = .top
view.addSubview(tabBar!) view.addSubview(tabBar!)
prepareTabBarButtons(buttons) prepareTabBarButtons(buttons)
} }
...@@ -350,10 +353,6 @@ extension PageMenuController { ...@@ -350,10 +353,6 @@ extension PageMenuController {
- Parameter position: An Int for the position of the view controller. - Parameter position: An Int for the position of the view controller.
*/ */
fileprivate func layoutViewController(at index: Int, position: Int) { fileprivate func layoutViewController(at index: Int, position: Int) {
guard 0 <= index && index < viewControllers.count else {
return
}
viewControllers[index].view.frame = CGRect(x: CGFloat(position) * scrollView.width, y: 0, width: scrollView.width, height: scrollView.height) viewControllers[index].view.frame = CGRect(x: CGFloat(position) * scrollView.width, y: 0, width: scrollView.width, height: scrollView.height)
} }
} }
......
...@@ -57,10 +57,33 @@ public protocol TabBarDelegate { ...@@ -57,10 +57,33 @@ public protocol TabBarDelegate {
optional func tabBar(tabBar: TabBar, didSelect button: UIButton) optional func tabBar(tabBar: TabBar, didSelect button: UIButton)
} }
@objc(TabBarStyle)
public enum TabBarStyle: Int {
case normal
case scrollable
}
open class TabBar: Bar { open class TabBar: Bar {
/// A boolean indicating if the TabBar line is in an animation state. /// A boolean indicating if the TabBar line is in an animation state.
open internal(set) var isAnimating = false open internal(set) var isAnimating = false
/// Enables and disables bouncing when swiping.
open var isBounceEnabled: Bool {
get {
return contentView.bounces
}
set(value) {
contentView.bounces = value
}
}
/// An enum that determines the tab bar style.
open var tabBarStyle = TabBarStyle.scrollable {
didSet {
layoutSubviews()
}
}
/// A delegation reference. /// A delegation reference.
open weak var delegate: TabBarDelegate? open weak var delegate: TabBarDelegate?
...@@ -116,8 +139,6 @@ open class TabBar: Bar { ...@@ -116,8 +139,6 @@ open class TabBar: Bar {
b.removeFromSuperview() b.removeFromSuperview()
} }
centerViews = buttons as [UIView]
layoutSubviews() layoutSubviews()
} }
} }
...@@ -186,9 +207,26 @@ open class TabBar: Bar { ...@@ -186,9 +207,26 @@ open class TabBar: Bar {
} }
} }
contentView.grid.axis.columns = buttons.count if .scrollable == tabBarStyle {
contentView.grid.reload() var w: CGFloat = 0
for b in buttons {
let width = b.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: contentView.height)).width + interimSpace
contentView.addSubview(b)
b.height = contentView.height
b.width = width
b.x = w
w += width
}
print(w, contentView.width)
if w > contentView.width {
contentView.contentSize.width = w
}
} else {
contentView.grid.axis.columns = buttons.count
centerViews = buttons
}
if nil == selected { if nil == selected {
selected = buttons.first selected = buttons.first
} }
...@@ -205,8 +243,9 @@ open class TabBar: Bar { ...@@ -205,8 +243,9 @@ open class TabBar: Bar {
*/ */
open override func prepare() { open override func prepare() {
super.prepare() super.prepare()
isBounceEnabled = true
contentEdgeInsetsPreset = .none contentEdgeInsetsPreset = .none
interimSpacePreset = .none interimSpacePreset = .interimSpace5
prepareLine() prepareLine()
prepareDivider() prepareDivider()
} }
......
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