Xlpagertabstrip: Move to View Controller without animation when the view is not loaded yet

Created on 1 Nov 2017  Â·  8Comments  Â·  Source: xmartlabs/XLPagerTabStrip

When I call the moveToViewController(at:) when view is not loaded, then in next call of PagerTabStripViewController.viewDidAppear(_ animated:) method is the variable needToUpdateCurrentChild set to true and the moveToViewController(at:) is re-called with default animated parameter (true). This means that I can't move to other view controller without animation when the view is not loaded at the moment when I called moveToViewController(at:).

Maybe PagerTabStripViewController should store the animated parameter from moveToViewController(at:) call and then re-call this method in viewDidAppear(_ animated:) with this stored parameter. Is this a bad idea?

Most helpful comment

+1

I think this is one of the main issues when updating to the new release. We should definitely have a way to move to the page we want at the loading phase, before the view is rendered.

All 8 comments

+1

I think this is one of the main issues when updating to the new release. We should definitely have a way to move to the page we want at the loading phase, before the view is rendered.

This is presenting a challenge for me also, I want to have the last tab be the active one by default (seems odd, but necessary for my particular use case), only place I can do it is in viewDidAppear(), but that causes a visual flicker which is less than ideal

I have the same issue, i cant make second tab by default, any solution ?

Whether there is a solution?

Any ideas how to do this without the viewDidAppear method?

open func moveToViewController(at index: Int, animated: Bool = true) {
    guard isViewLoaded && view.window != nil && currentIndex != index else {
        currentIndex = index
        return
    }
    if animated && pagerBehaviour.skipIntermediateViewControllers && abs(currentIndex - index) > 1 {
        var tmpViewControllers = viewControllers
        let currentChildVC = viewControllers[currentIndex]
        let fromIndex = currentIndex < index ? index - 1 : index + 1
        let fromChildVC = viewControllers[fromIndex]
        tmpViewControllers[currentIndex] = fromChildVC
        tmpViewControllers[fromIndex] = currentChildVC
        pagerTabStripChildViewControllersForScrolling = tmpViewControllers
        containerView.setContentOffset(CGPoint(x: pageOffsetForChild(at: fromIndex), y: 0), animated: false)
        (navigationController?.view ?? view).isUserInteractionEnabled = !animated
        containerView.setContentOffset(CGPoint(x: pageOffsetForChild(at: index), y: 0), animated: true)
    }
    else {
        (navigationController?.view ?? view).isUserInteractionEnabled = !animated
        containerView.setContentOffset(CGPoint(x: pageOffsetForChild(at: index), y: 0), animated: animated)
    }
}

I forked the repository and changed the method on line 151 in class PageTabStripViewController with the one above this text. They are using pre-index variable to set the index where the viewController should move after the view is loaded, since my use case is different I changed it with the current index and it works fine for now

@marinnikolic That work's like a charm. However while the current tab is being opened, the corresponding page indicator for it is not updated

I mad workaround for this, I changed currentIndex property to be writable in class PagerTabStripViewController , and set the index of tab to it in viewDidLoad method before calling super.viewDidLoad() and worked fine.

like this

 override func viewDidLoad() {

        if openTab2 {
            self.currentIndex = 1
        } else if openTab3 {
            self.currentIndex = 2
        }
        super.viewDidLoad()

}
Was this page helpful?
0 / 5 - 0 ratings