Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PageViewController current page index in Swift

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?  } 
like image 604
LeNI Avatar asked Sep 28 '15 12:09

LeNI


People also ask

How to use uipageviewcontroller in iOS using swift?

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.

How to add the viewcontroller for page content in uipageviewcontroller?

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.

What is uipageviewcontroller in Salesforce?

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.

How to add image view in UIViewController in SharePoint?

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.


2 Answers

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 } 
like image 193
manukv Avatar answered Oct 11 '22 14:10

manukv


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 } 
like image 38
Igor Avatar answered Oct 11 '22 15:10

Igor