【问题标题】:UIPageViewController dots not showingUIPageViewController 点不显示
【发布时间】:2019-11-09 01:55:27
【问题描述】:

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

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
    }

}

【问题讨论】:

  • 你在用UIPageViewControllerTransitionStyleScroll
  • 在故事板中我修复了它,查看我更新的问题
  • 这个方法presentationCountForPageViewController被调用了?
  • 不不打电话....
  • presentationCountForPageViewController 在数据源中,所以没问题。如果您从教程中复制粘贴代码,请确保它符合最新的 swift 语法

标签: ios swift xcode cocoa-touch uipageviewcontroller


【解决方案1】:

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

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

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

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

【讨论】:

  • 是的,它可以工作,但背景变成黑色。如何删除
  • 将 UIPageControl backgroundColor 设置为白色:UIPageControl.appearance().backgroundColor = UIColor.white
  • @Hitesh Borse,谢谢,但它不能满足我的要求,你能建议如何将 PageViewController 添加到现有的 ViewController 并加载另外三个 viewcontroller 吗?你能为我推荐任何代码或教程吗?
  • 谢谢你,这个代码对我有用,只是做了一些改动,samwize.com/2016/03/08/…
【解决方案2】:

您应该添加页面视图控制器数据源和页面数: pageControl.numberOfPages = 3

【讨论】:

    猜你喜欢
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多