XLPagerTabStrip icon indicating copy to clipboard operation
XLPagerTabStrip copied to clipboard

Set default tab or index

Open BasantAshraf opened this issue 6 years ago • 14 comments

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)
}

BasantAshraf avatar Feb 07 '18 21:02 BasantAshraf

im having this same issue, can't seem to get a solution

bradleyvandyk avatar Feb 27 '18 21:02 bradleyvandyk

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

bradleyvandyk avatar Feb 27 '18 21:02 bradleyvandyk

Any news ? i have same problem

OuSS-90 avatar Mar 13 '18 13:03 OuSS-90

Any updates on this problem?

smilesworld116 avatar May 08 '18 06:05 smilesworld116

Just found a solution after some tackles. viewWillLayoutSubviews() does the job!

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    moveToViewController(at: customIndexToMove)
}

smilesworld116 avatar May 08 '18 06:05 smilesworld116

override func viewDidLoad() {
    super.viewDidLoad()
      	DispatchQueue.main.async {
           	self.moveToViewController(at: 3, animated: false)
    }
}

dexter199402 avatar May 30 '18 00:05 dexter199402

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)
}

ReverseScale avatar Dec 11 '18 02:12 ReverseScale

Исходя из этого, вам нужна последняя версия библиотеки, это очень полезно для меня. Если вам нужна анимация

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 avatar Feb 20 '19 11:02 Ultraa

@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.

417-72KI avatar Feb 28 '19 03:02 417-72KI

@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.

Ultraa avatar Mar 05 '19 09:03 Ultraa

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 avatar Jul 15 '19 10:07 zhpengkun

@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()
   }
}

mistahenry avatar Sep 25 '19 08:09 mistahenry

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
    }
}

niusounds avatar Jun 02 '20 09:06 niusounds

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()
		}
	}
}

denizmersinlioglu avatar Jan 08 '22 19:01 denizmersinlioglu