TabBar: Smarter content offset adjustment

This commit is contained in:
James Magahern
2021-02-15 23:18:52 -08:00
parent 101f8f17e7
commit 372a0f32e9

View File

@@ -134,7 +134,7 @@ class TabBarView: UIView
private var tabViews: [TabView] = []
private var activeTabIndex: Int = 0
private var tabContainerView = UIScrollView(frame: .zero)
private var needsScrollAdjustment = false
private let bottomSeparatorView = UIView(frame: .zero)
private let placeholderTabImage = UIImage(systemName: "network")
@@ -198,9 +198,7 @@ class TabBarView: UIView
})
}
// Adjust scroll offset
let contentEnd = CGPoint(x: tabContainerView.contentSize.width - tabContainerView.bounds.width, y: 0.0)
tabContainerView.setContentOffset(contentEnd, animated: animated)
needsScrollAdjustment = true
}
public func reloadTab(atIndex index: Int) {
@@ -312,5 +310,15 @@ class TabBarView: UIView
tabContainerView.contentSize = CGSize(
width: xOffset, height: tabContainerBounds.height
)
// Adjust content offset, if needed
if needsScrollAdjustment {
if let activeTabView = tabViews.first(where: { $0.active }) {
if activeTabView.frame.maxX > (tabContainerView.contentOffset.x + tabContainerBounds.width) {
let contentOffset = min(tabContainerView.contentSize.width - tabContainerView.bounds.width, activeTabView.frame.maxX - tabContainerBounds.width)
tabContainerView.setContentOffset(CGPoint(x: contentOffset, y: 0), animated: true)
}
}
}
}
}