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

Set default tab or index #537

Open
BasantAshraf opened this issue Feb 7, 2018 · 14 comments
Open

Set default tab or index #537

BasantAshraf opened this issue Feb 7, 2018 · 14 comments

Comments

@BasantAshraf
Copy link

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)
}
@bradleyvandyk
Copy link

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

@bradleyvandyk
Copy link

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

@OuSS-90
Copy link

OuSS-90 commented Mar 13, 2018

Any news ? i have same problem

@smilesworld116
Copy link

Any updates on this problem?

@smilesworld116
Copy link

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

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

@dexter199402
Copy link

dexter199402 commented May 30, 2018

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

@ReverseScale
Copy link

ReverseScale commented Dec 11, 2018

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

@Ultraa
Copy link

Ultraa commented Feb 20, 2019

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

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.

@417-72KI
Copy link

@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
Copy link

Ultraa commented Mar 5, 2019

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

@zhpengkun
Copy link

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.

@mistahenry
Copy link

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

@niusounds
Copy link

#537 (comment) 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
    }
}

@denizmersinlioglu
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests