Commit 610a1db9 by Daniel Dahan

issue-815: fixed alignment issues on rotation

parent 74b26971
...@@ -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.8.0</string> <string>2.8.1</string>
<key>CFBundleSignature</key> <key>CFBundleSignature</key>
<string>????</string> <string>????</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
......
...@@ -179,10 +179,6 @@ open class Bar: View { ...@@ -179,10 +179,6 @@ open class Bar: View {
open override func layoutSubviews() { open override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
layoutBarSubviews()
}
open func layoutBarSubviews() {
guard willLayout else { guard willLayout else {
return return
} }
......
...@@ -142,8 +142,8 @@ open class SearchBar: Bar { ...@@ -142,8 +142,8 @@ open class SearchBar: Bar {
super.init(frame: frame) super.init(frame: frame)
} }
open override func layoutBarSubviews() { open override func layoutSubviews() {
super.layoutBarSubviews() super.layoutSubviews()
guard willLayout else { guard willLayout else {
return return
} }
......
...@@ -81,8 +81,8 @@ open class Snackbar: Bar { ...@@ -81,8 +81,8 @@ open class Snackbar: Bar {
return super.hitTest(point, with: event) return super.hitTest(point, with: event)
} }
open override func layoutBarSubviews() { open override func layoutSubviews() {
super.layoutBarSubviews() super.layoutSubviews()
guard willLayout else { guard willLayout else {
return return
} }
......
...@@ -68,6 +68,17 @@ open class TabBar: Bar { ...@@ -68,6 +68,17 @@ 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 fileprivate(set) var isAnimating = false open fileprivate(set) var isAnimating = false
/// The total width of the buttons.
fileprivate var buttonsTotalWidth: CGFloat {
var w: CGFloat = 0
for v in buttons {
w += v.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: contentView.height)).width + interimSpace
}
return w
}
/// Enables and disables bouncing when swiping. /// Enables and disables bouncing when swiping.
open var isBounceEnabled: Bool { open var isBounceEnabled: Bool {
get { get {
...@@ -150,6 +161,7 @@ open class TabBar: Bar { ...@@ -150,6 +161,7 @@ open class TabBar: Bar {
b.removeFromSuperview() b.removeFromSuperview()
} }
prepareButtons()
layoutSubviews() layoutSubviews()
} }
} }
...@@ -198,7 +210,8 @@ open class TabBar: Bar { ...@@ -198,7 +210,8 @@ open class TabBar: Bar {
} }
} }
open override func layoutBarSubviews() { open override func layoutSubviews() {
super.layoutSubviews()
guard willLayout else { guard willLayout else {
return return
} }
...@@ -273,21 +286,9 @@ open class TabBar: Bar { ...@@ -273,21 +286,9 @@ open class TabBar: Bar {
} }
grid.axis.columns = columns grid.axis.columns = columns
grid.commit()
contentView.grid.commit()
layoutDivider()
var w: CGFloat = 0
for v in buttons { if .scrollable == tabBarStyle || (.auto == tabBarStyle && buttonsTotalWidth > bounds.width) {
w += v.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: contentView.height)).width + interimSpace var w: CGFloat = 0
}
if .scrollable == tabBarStyle || (w > bounds.width && .auto == tabBarStyle) {
scrollView.frame = CGRect(x: l, y: 0, width: p, height: height)
w = 0
for v in buttons { for v in buttons {
let x = v.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: contentView.height)).width + interimSpace let x = v.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: contentView.height)).width + interimSpace
scrollView.addSubview(v) scrollView.addSubview(v)
...@@ -298,40 +299,24 @@ open class TabBar: Bar { ...@@ -298,40 +299,24 @@ open class TabBar: Bar {
} }
scrollView.contentSize = CGSize(width: w, height: height) scrollView.contentSize = CGSize(width: w, height: height)
scrollView.addSubview(line)
} else { } else {
contentView.grid.axis.columns = buttons.count scrollView.grid.views = buttons
centerViews = buttons scrollView.grid.axis.columns = buttons.count
addSubview(line) scrollView.contentSize = CGSize(width: scrollView.width, height: height)
} }
updateSelectionLine() grid.commit()
contentView.grid.commit()
layoutDivider()
layoutLine()
} }
private func updateSelectionLine() {
for b in buttons {
b.grid.columns = 0
b.cornerRadius = 0
b.contentEdgeInsets = .zero
if isLineAnimated {
prepareLineAnimationHandler(button: b)
}
}
scrollView.frame = contentView.bounds
if nil == selected {
selected = buttons.first
}
line.frame = CGRect(x: selected!.x, y: .bottom == lineAlignment ? height - lineHeight : 0, width: selected!.width, height: lineHeight)
}
open override func prepare() { open override func prepare() {
super.prepare() super.prepare()
contentEdgeInsetsPreset = .none contentEdgeInsetsPreset = .none
interimSpacePreset = .interimSpace6 interimSpacePreset = .interimSpace6
prepareContentView() prepareContentView()
prepareScrollView() prepareScrollView()
prepareDivider() prepareDivider()
...@@ -353,6 +338,19 @@ fileprivate extension TabBar { ...@@ -353,6 +338,19 @@ fileprivate extension TabBar {
dividerAlignment = .top dividerAlignment = .top
} }
/// Prepares the buttons.
func prepareButtons() {
for v in buttons {
v.grid.columns = 0
v.cornerRadius = 0
v.contentEdgeInsets = .zero
if isLineAnimated {
prepareLineAnimationHandler(button: v)
}
}
}
/** /**
Prepares the line animation handlers. Prepares the line animation handlers.
- Parameter button: A UIButton. - Parameter button: A UIButton.
...@@ -372,7 +370,25 @@ fileprivate extension TabBar { ...@@ -372,7 +370,25 @@ fileprivate extension TabBar {
scrollView.isPagingEnabled = false scrollView.isPagingEnabled = false
scrollView.showsVerticalScrollIndicator = false scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false scrollView.showsHorizontalScrollIndicator = false
contentView.addSubview(scrollView) scrollView.addSubview(line)
centerViews = [scrollView]
}
}
fileprivate extension TabBar {
/// Layout the line view.
func layoutLine() {
guard 0 < buttons.count else {
return
}
if nil == selected {
selected = buttons.first
}
line.animate(.duration(0),
.size(CGSize(width: selected!.width, height: lineHeight)),
.position(CGPoint(x: selected!.center.x, y: .bottom == lineAlignment ? height - lineHeight / 2 : lineHeight / 2)))
} }
} }
......
...@@ -166,43 +166,35 @@ open class TabsController: UIViewController { ...@@ -166,43 +166,35 @@ open class TabsController: UIViewController {
view.contentScaleFactor = Screen.scale view.contentScaleFactor = Screen.scale
prepareContainer() prepareContainer()
prepareTabBar() prepareTabBar()
prepareTabBarButtons()
prepareViewControllers() prepareViewControllers()
} }
} }
fileprivate extension TabsController { fileprivate extension TabsController {
/** /// Prepares the container view.
Prepares the tabBar buttons. func prepareContainer() {
- Parameter _ buttons: An Array of UIButtons. view.addSubview(container)
*/
func prepareTabBarButtons(_ buttons: [UIButton]) {
tabBar.buttons = buttons
for b in tabBar.buttons {
b.removeTarget(self, action: #selector(handleTabBarButton(button:)), for: .touchUpInside)
b.addTarget(self, action: #selector(handleTabBarButton(button:)), for: .touchUpInside)
}
} }
/// Prepares the TabBar. /// Prepares the TabBar.
func prepareTabBar() { func prepareTabBar() {
tabBar.lineAlignment = .bottom == tabBarAlignment ? .top : .bottom
view.addSubview(tabBar)
}
/// Prepares the tabBar buttons.
func prepareTabBarButtons() {
var buttons = [UIButton]() var buttons = [UIButton]()
for v in viewControllers { for v in viewControllers {
let button = v.tabItem as UIButton let b = v.tabItem
v.isMotionEnabled = true b.removeTarget(self, action: #selector(handleTabBarButton(button:)), for: .touchUpInside)
buttons.append(button) b.addTarget(self, action: #selector(handleTabBarButton(button:)), for: .touchUpInside)
buttons.append(b)
} }
tabBar.lineAlignment = .bottom == tabBarAlignment ? .top : .bottom tabBar.buttons = buttons
view.addSubview(tabBar)
prepareTabBarButtons(buttons)
}
/// Prepares the container view.
func prepareContainer() {
view.addSubview(container)
} }
/// Prepares all the view controllers. /// Prepares all the view controllers.
...@@ -212,6 +204,7 @@ fileprivate extension TabsController { ...@@ -212,6 +204,7 @@ fileprivate extension TabsController {
continue continue
} }
viewControllers[i].view.isHidden = true
prepareViewController(at: i) prepareViewController(at: i)
} }
...@@ -232,6 +225,7 @@ fileprivate extension TabsController { ...@@ -232,6 +225,7 @@ fileprivate extension TabsController {
addChildViewController(vc) addChildViewController(vc)
vc.didMove(toParentViewController: self) vc.didMove(toParentViewController: self)
vc.isMotionEnabled = true
vc.view.clipsToBounds = true vc.view.clipsToBounds = true
vc.view.contentScaleFactor = Screen.scale vc.view.contentScaleFactor = Screen.scale
container.addSubview(vc.view) container.addSubview(vc.view)
...@@ -328,6 +322,7 @@ fileprivate extension TabsController { ...@@ -328,6 +322,7 @@ fileprivate extension TabsController {
let fvc = viewControllers[selectedIndex] let fvc = viewControllers[selectedIndex]
let tvc = viewControllers[i] let tvc = viewControllers[i]
tvc.view.isHidden = false
tvc.view.frame.size = container.bounds.size tvc.view.frame.size = container.bounds.size
tvc.motionModalTransitionType = motionTransitionType tvc.motionModalTransitionType = motionTransitionType
......
...@@ -95,8 +95,8 @@ open class Toolbar: Bar { ...@@ -95,8 +95,8 @@ open class Toolbar: Bar {
contentViewAlignment = .center == titleLabel.textAlignment ? .center : .full contentViewAlignment = .center == titleLabel.textAlignment ? .center : .full
} }
open override func layoutBarSubviews() { open override func layoutSubviews() {
super.layoutBarSubviews() super.layoutSubviews()
guard willLayout else { guard willLayout 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