17

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!

rounak
  • 9,217
  • 3
  • 42
  • 59
Pompey Casmilus
  • 205
  • 2
  • 4
  • 7

6 Answers6

30

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
}
Zell B.
  • 10,266
  • 3
  • 40
  • 49
  • I put a println statement to debug inside my presentationCountForPageViewController method and it never gets called. That could be why my page counter dots are not showing - but why doesn't it get called? Is a Page Counter automatically created with UIPageViewController? – Pompey Casmilus Jun 19 '15 at 19:43
  • You never add pageController.view as a subview of self.view. – jrc Sep 04 '15 at 17:29
  • 13
    @PompeyCasmilus You might be using the wrong version of those functions. All the Apple frameworks methods had their names refactored to match the new Swift naming convention. It's happened to me a lot that I called the wrong version. – Suman Roy Mar 15 '17 at 14:15
  • 5
    Just a heads up, although the question already has this, a gotcha is you need to set ```transitionStyle: .scroll, navigationOrientation: .horizontal``` in order for the above methods to work. – Herbal7ea Mar 22 '17 at 19:35
  • 8
    For Swift 3 the func name must be exactly: presentationCount(for pageViewController: UIPageViewController) – Sean Dev Mar 29 '17 at 15:13
21

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
    }
Rafat touqir Rafsun
  • 2,777
  • 28
  • 24
11

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
milczi
  • 7,064
  • 2
  • 27
  • 22
5

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.

The Senator
  • 5,181
  • 2
  • 34
  • 49
1

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?

timeSmith
  • 553
  • 1
  • 8
  • 16
0

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

Nupur Sharma
  • 1,106
  • 13
  • 15