页面视图控制器不显示点

时间:2019-04-24 17:27:11

标签: ios swift uipageviewcontroller

我已经创建了一个滚动的pageviewcontroller,但是我无法显示点/条。根据我的理解,只要将Navigation设置为Horizo​​ntal并将Transition样式设置为Scroll,默认情况下应该存在此选项,但是什么也没有显示。

然后,我添加了一些其他代码来添加和设置点的样式,但仍然一无所获。这是我目前的代码:

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)
    }
}

我什至继续创建了一个新的空白页面视图控制器,以完全不使用任何代码进行测试。我只是将“导航”设置为“水平”,将“过渡”设置为“滚动”,仅此而已。没有代码或大惊小怪。我使用“用户定义的运行时属性”将背景色设置为随机的蓝色,这样我就可以看到导航/点栏(如果有的话),但是我什么也没得到。

我想念什么?

3 个答案:

答案 0 :(得分:1)

  

根据我的理解,只要将“导航”设置为“水平”并且将“过渡样式”设置为“滚动”,默认情况下就应该存在此提示。

您的理解是错误的。

不添加页面控件。它是内置的。只要实现以下两种方法,它将显示出来:

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

不幸的是,内置的页面控制点是白色的,这使得它们很难看到。使用UIAppearance代理进行修复(通常在applicationDidFinishLaunching中)。例如:

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

至少通过这种方式,您会看到页面控件存在,并且可以修改细节以适合需要。

答案 1 :(得分:0)

我认为您想从presentationCountForPageViewController实现presentationIndexForPageViewControllerUIPageViewControllerDataSource

答案 2 :(得分:0)

尝试以下声明:

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

并删除self.view.addSubview(pageControl)