Swift/iOS8: Why are Page Control indicators not showing?

Pompey Casmilus picture Pompey Casmilus · Jun 19, 2015 · Viewed 16.6k times · Source

I am implementing a straightforward gallery view controller where the app displays a small range of full-screen images that the user can scroll through. I'm using UIPageViewController which I thought, should display the Page Control indicators automatically if I implement the correct datasource functions. However I still cannot see any indicators.

In my main GalleryViewController:

class GalleryViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

var pageController: UIPageViewController?
var pageContent = NSArray()

override func viewDidLoad() {
super.viewDidLoad()
.... some code to load images into the pageContent array ....

pageController = UIPageViewController(transitionStyle:.Scroll, navigationOrientation:.Horizontal,options:nil) 
pageController?.delegate = self
pageController?.dataSource = self

... some more standard code to add the page controller to self.view etc.

var pageControl = UIPageControl.appearance()
pageControl.pageIndicatorTintColor = UIColor.lightGray()
pageControl.currentPageIndicatorTintColor = UIColor.whiteColor()
pageControl.backgroundColor = UIColor.blackColor()

pageController!.didMoveToParentViewController(self)
}

func viewControllerAtIndex(index:Int) -> ContentViewController? {
....
}

I have implemented the two compulsory protocol methods viewControllerBeforeViewController and viewControllerAfterController as well as the two required ones for paging, presentationCountForPagesViewController and presentationIndexForPageViewController.

My ContentViewController has a UIImageView that fills the entire screen. However even when I decrease its size off the bottom of the screen, I cannot see any page indicator lights. Have I missed something here? Let me know if I have not provided enough information. I've been trying to rewrite an old app of mine that I coded in Objective-C over 5 years ago - entirely in Swift for iOS8 and Xcode etc. has changed so much, it is confusing. Thanks!

Answer

Zell B. picture Zell B. · Jun 19, 2015

Taken from here :

In order to show UIPageControl, you need to implement two optional datasource methods. Just return the whole number of pages for presentationCountForPageViewController and the initially selected index for presentationIndexForPageViewController

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return pageContent.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return 0
}