Case Silva
Case Silva

Reputation: 514

Page View Controller does not show dots

I have created a scrolling pageviewcontroller but I can not get the dots/bar to show up. From my understanding, this should be present by default as long as Navigation is set to Horizontal and Transition style is set to Scroll, but nothing is showing.

I then added some additional code to add and style the dots, but still nothing. Here is my code as it stands now:

import UIKit

class Swipable_Images: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate{
    var pages = [UIViewController]()
    var pageControl = UIPageControl()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        self.dataSource = self
        configurePageControl()


        let p1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "stop1_image1")
        let p2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "stop1_image2")

        pages.append(p1)
        pages.append(p2)

        setViewControllers([p1], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController)->UIViewController?{
        let cur = pages.index(of: viewController)!

        let prev = abs((cur - 1) % pages.count)
        return pages[prev]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController)->UIViewController?{
        let cur = pages.index(of: viewController)!

        let nxt = abs((cur+1) % pages.count)
        return pages[nxt]
    }

    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        return pages.count
    }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool){
        let pageContentViewController = pageViewController.viewControllers![0]
        self.pageControl.currentPage = pages.index(of: pageContentViewController)!
    }

    //Swipe Indicator Bar
    func configurePageControl(){
        pageControl = UIPageControl(frame: CGRect(x: self.view.center.x, y: self.view.center.y, width: UIScreen.main.bounds.width, height: 50))
        self.pageControl.numberOfPages = pages.count
        self.pageControl.currentPage = 0
        self.pageControl.tintColor = UIColor.black
        self.pageControl.pageIndicatorTintColor = UIColor.white
        self.pageControl.currentPageIndicatorTintColor = UIColor.black
        self.view.addSubview(pageControl)
    }
}

I even went ahead and created a new, blank page view controller to test with no code at all. I just set the Navigation to Horizontal and Transition to Scroll and nothing else. No code or fussing with it. I used a User Defined Runtime Attribute to set the background color to a random blue so that I would be able to see the nav/dot bar if it was there, but I'm still getting nothing.

What am I missing?

Upvotes: 0

Views: 2092

Answers (3)

M. Mansuelli
M. Mansuelli

Reputation: 1123

Try this declaration:

    pageControl = UIPageControl.appearance(whenContainedInInstancesOf: [Swipable_Images.self])

and remove self.view.addSubview(pageControl)

Upvotes: 0

matt
matt

Reputation: 535140

From my understanding, this should be present by default as long as Navigation is set to Horizontal and Transition style is set to Scroll, but nothing is showing.

Your understanding is wrong.

Do not add a page control. It is built in. It will show up provided you implement these two methods:

func presentationCount(for pvc: UIPageViewController) -> Int {
func presentationIndex(for pvc: UIPageViewController) -> Int {

Unfortunately the built in page control dots are white, which makes them hard to see. Use UIAppearance proxy to fix that (typically in applicationDidFinishLaunching). For example:

let proxy = UIPageControl.appearance()
proxy.pageIndicatorTintColor = UIColor.red.withAlphaComponent(0.6)
proxy.currentPageIndicatorTintColor = .red
proxy.backgroundColor = .yellow

At least this way you'll see that the page control is present and can modify the details to suit.

Upvotes: 1

Tom Pearson
Tom Pearson

Reputation: 384

I think you want to implement the presentationCountForPageViewController and presentationIndexForPageViewController from UIPageViewControllerDataSource

Upvotes: 0

Related Questions