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

19.1k views Asked by At

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!

6

There are 6 answers

5
Zell B. On BEST ANSWER

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
}
1
The Senator On

Although the answer appears in abundance in many of these answers, I just want to make it explicitly clear that you need to set the UIPageViewController to 'Scroll' before these methods will get hit.

4
Rafat touqir Rafsun On

Why not use swift 3+ version of Zell B.'s answer

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return self.providerItemList?.providerItems?.count ?? 0
    }

    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        return 0
    }
0
Nupur Sharma On

My mistake was to set the count value more than the actual content pages in my page view controller in presentationCount() delegate method.

2
milczi On

Even if you will change color of UIPageControl it may not appear. If UIPageController is inside the UITabBarController the TabBar will cover the UIPageControl. The best way to find it is use option Debug->View Debubging->CaptureViewHierarchy. Then in the left bottom filter field type "pageControl". If you click it in the navigator it should locate it on the screen (take a look at the screenshot)

enter image description here

You can change the position like this:

let controlHeight: CGFloat = 50
    let bottomSpace: CGFloat = 20
    let yPosition = view.frame.size.height - (controlHeight + bottomSpace)
    let fullScreenWidth = view.frame.size.width
    let frame = CGRect(x: 0, y: yPosition, width: fullScreenWidth, height: controlHeight)

    pageControl.frame = frame
0
timeSmith On

The dots are white, or semitransparent white. Make sure the background is not white, or change the colour of the dots.

How to edit the dots' color: How can I change the color of pagination dots of UIPageControl?