Commit ffd475b2 by Daniel Dahan

fixed line layout rotation issue for TabBar

parent 366df6a0
......@@ -228,7 +228,7 @@ open class Card: PulseView {
if 0 == h || nil != view as? UILabel {
(view as? UILabel)?.sizeToFit()
h = view.sizeThatFits(CGSize(width: w, height: CGFloat.greatestFiniteMagnitude)).height
h = view.sizeThatFits(CGSize(width: w, height: .greatestFiniteMagnitude)).height
}
view.width = w
......
......@@ -119,11 +119,17 @@ open class ChipBar: Bar {
/// The total width of the chipItems.
fileprivate var chipItemsTotalWidth: CGFloat {
var w: CGFloat = 0
let q = 2 * chipItemsInterimSpace
let p = q + chipItemsInterimSpace
for v in chipItems {
w += v.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: contentView.height)).width + interimSpace
let x = v.sizeThatFits(CGSize(width: .greatestFiniteMagnitude, height: scrollView.height)).width
w += x
w += p
}
w -= chipItemsInterimSpace
return w
}
......@@ -150,9 +156,6 @@ open class ChipBar: Bar {
/// A delegation reference.
open weak var delegate: ChipBarDelegate?
/// The currently selected chipItem.
open fileprivate(set) var selected: ChipItem?
/// A preset wrapper around chipItems contentEdgeInsets.
open var chipItemsContentEdgeInsetsPreset: EdgeInsetsPreset {
get {
......@@ -266,13 +269,15 @@ fileprivate extension ChipBar {
fileprivate extension ChipBar {
/// Layout the scrollView.
func layoutScrollView() {
if .scrollable == chipBarStyle || (.auto == chipBarStyle && chipItemsTotalWidth > bounds.width) {
contentView.grid.reload()
if .scrollable == chipBarStyle || (.auto == chipBarStyle && chipItemsTotalWidth > scrollView.width) {
var w: CGFloat = 0
let q = 2 * chipItemsInterimSpace
let p = q + chipItemsInterimSpace
for v in chipItems {
let x = v.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: scrollView.height)).width
let x = v.sizeThatFits(CGSize(width: .greatestFiniteMagnitude, height: scrollView.height)).width
v.height = scrollView.height
v.width = x + q
v.x = w
......@@ -296,7 +301,7 @@ fileprivate extension ChipBar {
scrollView.grid.contentEdgeInsets = chipItemsContentEdgeInsets
scrollView.grid.interimSpace = chipItemsInterimSpace
scrollView.grid.commit()
scrollView.contentSize = scrollView.bounds.size
scrollView.contentSize = scrollView.frame.size
}
}
}
......@@ -346,12 +351,11 @@ fileprivate extension ChipBar {
delegate?.chipBar?(chipBar: self, willSelect: chipItem)
}
selected = chipItem
isAnimating = true
if !scrollView.bounds.contains(chipItem.frame) {
let contentOffsetX = (chipItem.x < scrollView.bounds.minX) ? chipItem.x : chipItem.frame.maxX - scrollView.bounds.width
let normalizedOffsetX = min(max(contentOffsetX, 0), scrollView.contentSize.width - scrollView.bounds.width)
if !scrollView.frame.contains(chipItem.frame) {
let contentOffsetX = (chipItem.x < scrollView.frame.minX) ? chipItem.x : chipItem.frame.maxX - scrollView.width
let normalizedOffsetX = min(max(contentOffsetX, 0), scrollView.contentSize.width - scrollView.width)
scrollView.setContentOffset(CGPoint(x: normalizedOffsetX, y: 0), animated: true)
}
}
......
......@@ -78,11 +78,17 @@ open class TabBar: Bar {
/// The total width of the tabItems.
fileprivate var tabItemsTotalWidth: CGFloat {
var w: CGFloat = 0
let q = 2 * tabItemsInterimSpace
let p = q + tabItemsInterimSpace
for v in tabItems {
w += v.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: contentView.height)).width + interimSpace
let x = v.sizeThatFits(CGSize(width: .greatestFiniteMagnitude, height: scrollView.height)).width
w += x
w += p
}
w -= tabItemsInterimSpace
return w
}
......@@ -237,7 +243,6 @@ fileprivate extension TabBar {
func prepareTabItems() {
for v in tabItems {
v.grid.columns = 0
v.cornerRadius = 0
v.contentEdgeInsets = .zero
prepareLineAnimationHandler(tabItem: v)
......@@ -273,13 +278,15 @@ fileprivate extension TabBar {
fileprivate extension TabBar {
/// Layout the scrollView.
func layoutScrollView() {
if .scrollable == tabBarStyle || (.auto == tabBarStyle && tabItemsTotalWidth > bounds.width) {
contentView.grid.reload()
if .scrollable == tabBarStyle || (.auto == tabBarStyle && tabItemsTotalWidth > scrollView.width) {
var w: CGFloat = 0
let q = 2 * tabItemsInterimSpace
let p = q + tabItemsInterimSpace
for v in tabItems {
let x = v.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: scrollView.height)).width
let x = v.sizeThatFits(CGSize(width: .greatestFiniteMagnitude, height: scrollView.height)).width
v.height = scrollView.height
v.width = x + q
v.x = w
......@@ -303,7 +310,7 @@ fileprivate extension TabBar {
scrollView.grid.contentEdgeInsets = tabItemsContentEdgeInsets
scrollView.grid.interimSpace = tabItemsInterimSpace
scrollView.grid.commit()
scrollView.contentSize = scrollView.bounds.size
scrollView.contentSize = scrollView.frame.size
}
}
......@@ -313,7 +320,9 @@ fileprivate extension TabBar {
return
}
line.frame = CGRect(x: v.x, y: .bottom == lineAlignment ? height - lineHeight: 0, width: v.width, height: lineHeight)
line.animate(.duration(0),
.size(CGSize(width: v.width, height: lineHeight)),
.position(CGPoint(x: v.center.x, y: .bottom == lineAlignment ? height - lineHeight / 2 : lineHeight / 2)))
}
}
......@@ -392,9 +401,9 @@ fileprivate extension TabBar {
completion?(tabItem)
})
if !scrollView.bounds.contains(tabItem.frame) {
let contentOffsetX = (tabItem.x < scrollView.bounds.minX) ? tabItem.x : tabItem.frame.maxX - scrollView.bounds.width
let normalizedOffsetX = min(max(contentOffsetX, 0), scrollView.contentSize.width - scrollView.bounds.width)
if !scrollView.frame.contains(tabItem.frame) {
let contentOffsetX = (tabItem.x < scrollView.frame.minX) ? tabItem.x : tabItem.frame.maxX - scrollView.width
let normalizedOffsetX = min(max(contentOffsetX, 0), scrollView.contentSize.width - scrollView.width)
scrollView.setContentOffset(CGPoint(x: normalizedOffsetX, y: 0), animated: true)
}
}
......
......@@ -161,6 +161,7 @@ open class TabBarController: UIViewController {
open func prepare() {
view.backgroundColor = .white
view.contentScaleFactor = Screen.scale
prepareTabBar()
prepareTabBarItems()
prepareContainer()
......
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