I want to get current index of a pageViewController, I don't know how I get the visible pages index.
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool,previousViewControllers: [UIViewController],transitionCompleted completed: Bool) { // How to get it? }
This blog post gives you an idea about how to use UIPageViewController in iOS using Swift. Each page is managed by its own view controller object. The screen navigation is controlled by the user gestures. Once UIPageViewController interface is defined, you can add the ViewController for page content.
The screen navigation is controlled by the user gestures. Once UIPageViewController interface is defined, you can add the ViewController for page content. Click here for more information about UIPageViewController.
A UIPageViewController lets user navigate between pages of content, In which: Each page is managed by its own view controller object. The screen navigation is controlled by the user gestures. Once UIPageViewController interface is defined, you can add the ViewController for page content.
Open Main.storyboard and select UIViewController and assign PageContentViewController class reference to it. Drag Image view from object library to PageContentViewController and apply constraints to image view.
You can use didFinishAnimating, and set tags to viewcontrollers. try this
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { if (!completed) { return } self.pageControl.currentPageIndex = pageViewController.viewControllers!.first!.view.tag //Page Index }
Add this code to your UIPageViewController
.
var pages = [UIViewController]() var currentIndex: Int { guard let vc = viewControllers?.first else { return 0 } return pages.firstIndex(of: vc) ?? 0 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With