XLPagerTabStrip
XLPagerTabStrip copied to clipboard
Set default tab or index
Using "moveToViewController" causes slider to be animated and moved from first tab to tab 3 how to make it set by default to tab 3 without moving ?
override func viewDidAppear(_ animated: Bool) {
self.moveToViewController(at: 3)
}
im having this same issue, can't seem to get a solution
it's supposed to set preCurrentIndex = index if isViewLoaded is false, which it does, but that doesn't seem to actually set the default index
Any news ? i have same problem
Any updates on this problem?
Just found a solution after some tackles. viewWillLayoutSubviews()
does the job!
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
moveToViewController(at: customIndexToMove)
}
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.moveToViewController(at: 3, animated: false)
}
}
Premise, you need the latest version of the library,This is very useful to me. If you need animation
override func viewDidLoad() {
super.viewDidLoad()
moveToViewController(at: 3)
}
If no animation is needed
override func viewDidLoad() {
super.viewDidLoad()
buttonBarView.moveTo(index: 3, animated: false, swipeDirection: .none, pagerScroll: .no)
}
Исходя из этого, вам нужна последняя версия библиотеки, это очень полезно для меня. Если вам нужна анимация
override func viewDidLoad() { super.viewDidLoad() moveToViewController(at: 3) }
Если анимация не нужна
override func viewDidLoad() { super.viewDidLoad() buttonBarView.moveTo(index: 3, animated: false, swipeDirection: .none, pagerScroll: .no) }
@ReverseScale , i have tried both variants in version 8.1.1, but it isn't works for me. I always make transition to first viewcontroller in collection.
@Ultraa
How about using DispatchQueue.main.async
like @dexter199402 did?
override func viewDidLoad() { super.viewDidLoad() DispatchQueue.main.async { self.moveToViewController(at: 3, animated: false) } }
I tried and succeed.
BTW, I tried
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.buttonBarView.moveTo(index: 3, animated: false, swipeDirection: .none, pagerScroll: .no)
}
}
then only tab moves to specified, but view controller didn't switch.
@Ultraa How about using
DispatchQueue.main.async
like @dexter199402 did?override func viewDidLoad() { super.viewDidLoad() DispatchQueue.main.async { self.moveToViewController(at: 3, animated: false) } }
I tried and succeed.
BTW, I tried
override func viewDidLoad() { super.viewDidLoad() DispatchQueue.main.async { self.buttonBarView.moveTo(index: 3, animated: false, swipeDirection: .none, pagerScroll: .no) } }
then only tab moves to specified, but view controller didn't switch.
Thank you for answer, i used code like @dexter199402 . It's succeed, but causes a sharp visible jump when opening XLPagerTabController.
override func viewDidLoad() { super.viewDidLoad() DispatchQueue.main.async { self.moveToViewController(at: 3, animated: false) } }
This is not a perfect solution, it can change currentIndex when controller is shown, but it cause controller's didLoad method. And in my test, when call moveToViewController the first button bar title is always selected.
@zhpengkun my current workaround also fixes the initial menu being incorrectly set
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
DispatchQueue.main.async {
self.moveToViewController(at: 3, animated: false)
// needed or first tab stays highlighted
self.reloadPagerTabStripView()
}
}
https://github.com/xmartlabs/XLPagerTabStrip/issues/537#issuecomment-387295788 almost works fine. But viewDidLayoutSubviews
is called while navigating to other ViewControllers too. So I wrote:
private var initialized = false
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if initialized == false {
moveToViewController(at: 3, animated: false)
initialized = true
}
}
You can call to buttonBarView.reloadData()
after moveToViewController(at: targetIndex, animated: false)
in order to remove highlight from first item.
@mistahenry's solution works, however its loads target ViewController twice. It may create some unwanted behaviors.
class BaseTabPagerController: ButtonBarPagerTabStripViewController {
// MARK: Properties
var pagerHeight: CGFloat { 40 }
var initialPageIndex: Int { 0 }
var shouldLayoutPager = true
var topAnchor: NSLayoutYAxisAnchor { view.safeAreaLayoutGuide.topAnchor }
var bottomAnchor: NSLayoutYAxisAnchor { view.bottomAnchor }
// MARK: Life Cycle
override func viewDidLoad() {
configureBarViewStyle()
super.viewDidLoad()
configureBarViewFeatures()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
configurePagerViewIfNeeded()
}
// MARK: Configuration
func configureBarViewFeatures() {
// configure bar view behavior
}
func configureBarViewStyle() {
// configure bar view style
}
func configurePagerViewIfNeeded() {
guard shouldLayoutPager else { return }
shouldLayoutPager = false
// Layout pager subviews
let guide = view.safeAreaLayoutGuide
buttonBarView.anchor(top: topAnchor, leading: guide.leadingAnchor, trailing: guide.trailingAnchor, height: pagerHeight )
containerView.anchor(top: buttonBarView.bottomAnchor, leading: guide.leadingAnchor, bottom: bottomAnchor, trailing: guide.trailingAnchor)
// Move pager to target page if needed
if initialPageIndex != 0 {
moveToViewController(at: initialPageIndex, animated: false)
buttonBarView.reloadData()
}
}
}