Xlpagertabstrip: Set default tab or index

Created on 7 Feb 2018  ·  13Comments  ·  Source: xmartlabs/XLPagerTabStrip

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

Most helpful comment

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

All 13 comments

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
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

emrecanozturk picture emrecanozturk  ·  5Comments

Mohammed-Eliass picture Mohammed-Eliass  ·  4Comments

pramodshukla1011 picture pramodshukla1011  ·  3Comments

rkittinger picture rkittinger  ·  3Comments

unittesting0 picture unittesting0  ·  3Comments