ios - UIPageViewController 点不显示

标签 ios swift xcode cocoa-touch uipageviewcontroller

一切正常,但不显示点。我的代码是...

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

最佳答案

您使用的是这些函数的错误版本

为了显示 UIPageControl,您需要实现两个可选的数据源方法。只需返回 presentationCount 的全部页数和 presentationIndex

的初始选择索引
func presentationCount(for pageViewController: UIPageViewController) -> Int {
    setupPageControl()
    return orderedViewControllers.count
    }

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

enter image description here

关于ios - UIPageViewController 点不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56784337/

相关文章:

ios - iOS 10 中的 Whatsapp 集成和 openURL 问题

ios - UIButton 按下不会调用函数

ios - iOS应用程序中的音频文件

ios - 如何在解析网页内容时单击按钮

ios - 我们在 iOS 上有哪个唯一设备标识符在安装/删除/重新安装应用程序时不会改变

ios - Xcode 5 : Multiple test targets in one scheme: "Simulator already in use"

ios - 为什么 Collection View 使用此代码加载中心轮播?

ios - 如何使用 AutoLayout 使我的 UIImage 的高度与我的 UILabel 的固有高度相同?

ios - 如何在 ARKit 中使用手势 Action 在 SCNNode 中添加标签节点和用户图像?

ios - 容器 View 操作