Commit 610a1db9 by Daniel Dahan

issue-815: fixed alignment issues on rotation

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