Naresh
Naresh

Reputation: 17902

UIPageViewController dots not showing

Everything working fine but dots not showing. My code is...

import UIKit

class MyPageViewController: UIPageViewController  {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    dataSource = self

    if let firstViewController = orderedViewControllers.first {
        setViewControllers([firstViewController],
                           direction: .forward,
                           animated: true,
                           completion: nil)
    }

}

private(set) lazy var orderedViewControllers: [UIViewController] = {

    return [self.newColoredViewController(color: ""),
            self.newColoredViewController(color: "Second"),
            self.newColoredViewController(color: "Third")]
}()

private func newColoredViewController(color: String) -> UIViewController {

    return UIStoryboard(name: "Main", bundle: nil) .
        instantiateViewController(withIdentifier: "\(color)ViewController")
}

}


// MARK: UIPageViewControllerDataSource

extension MyPageViewController: UIPageViewControllerDataSource {

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

        guard let viewControllerIndex = orderedViewControllers.firstIndex(of: viewController) else {
            return nil
        }

        let previousIndex = viewControllerIndex - 1

        guard previousIndex >= 0 else {
            return nil
        }

        guard orderedViewControllers.count > previousIndex else {
            return nil
        }

        return orderedViewControllers[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController,
                            viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.firstIndex(of: viewController) else {
            return nil
        }

        let nextIndex = viewControllerIndex + 1
        let orderedViewControllersCount = orderedViewControllers.count

        guard orderedViewControllersCount != nextIndex else {
            return nil
        }

        guard orderedViewControllersCount > nextIndex else {
            return nil
        }

        return orderedViewControllers[nextIndex]
    }

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

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

    private func setupPageControl() {
        let appearance = UIPageControl.appearance()
        appearance.pageIndicatorTintColor = UIColor.darkGray
        appearance.currentPageIndicatorTintColor = UIColor.red
        appearance.backgroundColor = UIColor.black
    }

}

enter image description here

enter image description here

Upvotes: 2

Views: 2730

Answers (2)

ZahraAsgharzade
ZahraAsgharzade

Reputation: 309

you should add page view controller data source and number of pages : pageControl.numberOfPages = 3

Upvotes: 0

Hitesh Borse
Hitesh Borse

Reputation: 479

You are using the wrong version of those functions

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

func presentationCount(for pageViewController: UIPageViewController) -> Int {
    setupPageControl()
    return orderedViewControllers.count
    }

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

enter image description here

Upvotes: 8

Related Questions