Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 248
Naresh Avatar asked Dec 23 '25 00:12

Naresh


1 Answers

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

like image 118
Hitesh Borse Avatar answered Dec 24 '25 12:12

Hitesh Borse