Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting for fixed width of ButtonBarView's selected bar. #676

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ settings.style.buttonBarRightContentInset: CGFloat?
// selected bar view is created programmatically so it's important to set up the following 2 properties properly
settings.style.selectedBarBackgroundColor = UIColor.black
settings.style.selectedBarHeight: CGFloat = 5
// Optional. If you want a fixed width, otherwise same as item's width.
settings.style.selectedBarWidth: CGFloat?

// each buttonBar item is a UICollectionView cell of type ButtonBarViewCell
settings.style.buttonBarItemBackgroundColor: UIColor?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public struct ButtonBarPagerTabStripSettings {

public var selectedBarBackgroundColor = UIColor.black
public var selectedBarHeight: CGFloat = 5
public var selectedBarWidth: CGFloat?
public var selectedBarVerticalAlignment: SelectedBarVerticalAlignment = .bottom

public var buttonBarItemBackgroundColor: UIColor?
Expand Down Expand Up @@ -154,6 +155,7 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
buttonBarView.selectedBar.backgroundColor = settings.style.selectedBarBackgroundColor

buttonBarView.selectedBarHeight = settings.style.selectedBarHeight
buttonBarView.selectedBarWidth = settings.style.selectedBarWidth
buttonBarView.selectedBarVerticalAlignment = settings.style.selectedBarVerticalAlignment

// register button bar item cell
Expand Down
22 changes: 17 additions & 5 deletions Sources/XLPagerTabStrip/ButtonBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public enum SelectedBarVerticalAlignment {
open class ButtonBarView: UICollectionView {

open lazy var selectedBar: UIView = { [unowned self] in
let bar = UIView(frame: CGRect(x: 0, y: self.frame.size.height - CGFloat(self.selectedBarHeight), width: 0, height: CGFloat(self.selectedBarHeight)))
let bar = UIView(frame: CGRect(x: 0, y: self.frame.size.height - CGFloat(self.selectedBarHeight), width: self.selectedBarWidth ?? 0, height: CGFloat(self.selectedBarHeight)))
bar.layer.zPosition = 9999
return bar
}()
Expand All @@ -56,6 +56,7 @@ open class ButtonBarView: UICollectionView {
updateSelectedBarYPosition()
}
}
internal var selectedBarWidth: CGFloat?
var selectedBarVerticalAlignment: SelectedBarVerticalAlignment = .bottom
var selectedBarAlignment: SelectedBarAlignment = .center
var selectedIndex = 0
Expand Down Expand Up @@ -97,8 +98,14 @@ open class ButtonBarView: UICollectionView {

var targetFrame = fromFrame
targetFrame.size.height = selectedBar.frame.size.height
targetFrame.size.width += (toFrame.size.width - fromFrame.size.width) * progressPercentage
targetFrame.origin.x += (toFrame.origin.x - fromFrame.origin.x) * progressPercentage
if let sbWidth = self.selectedBarWidth {
targetFrame.origin.x = (fromFrame.origin.x + (fromFrame.width - sbWidth) / 2)
+ ((toFrame.origin.x + toFrame.width / 2) - (fromFrame.origin.x + fromFrame.width / 2)) * progressPercentage
targetFrame.size.width = sbWidth
} else {
targetFrame.size.width += (toFrame.size.width - fromFrame.size.width) * progressPercentage
targetFrame.origin.x += (toFrame.origin.x - fromFrame.origin.x) * progressPercentage
}

selectedBar.frame = CGRect(x: targetFrame.origin.x, y: selectedBar.frame.origin.y, width: targetFrame.size.width, height: selectedBar.frame.size.height)

Expand All @@ -122,8 +129,13 @@ open class ButtonBarView: UICollectionView {

updateContentOffset(animated: animated, pagerScroll: pagerScroll, toFrame: selectedCellFrame, toIndex: (selectedCellIndexPath as NSIndexPath).row)

selectedBarFrame.size.width = selectedCellFrame.size.width
selectedBarFrame.origin.x = selectedCellFrame.origin.x
if let sbWidth = self.selectedBarWidth {
selectedBarFrame.size.width = sbWidth
selectedBarFrame.origin.x = selectedCellFrame.origin.x + (selectedCellFrame.width - sbWidth) / 2
} else {
selectedBarFrame.size.width = selectedCellFrame.size.width
selectedBarFrame.origin.x = selectedCellFrame.origin.x
}

if animated {
UIView.animate(withDuration: 0.3, animations: { [weak self] in
Expand Down