ios - 在 UIPageViewController swift 的最后一页隐藏点

标签 ios swift swift2 uipageviewcontroller uipagecontrol

我在 UIPageViewController 中有四个页面,我想隐藏最后一页上的点。我成功地制作了一个在 UIPageViewController 的最后一页上调用的函数。基本上,如果当前 View 是最后一个,它就会被调用。但是我要在该函数中输入什么来暂时隐藏这些点?

我找到了这个 https://stackoverflow.com/a/32016614/5700898但它对该函数内部的内容没有帮助。普通方法(隐藏所有页面上的页面点)不是我需要的。

如何仅在 UIPageViewController 的最后一页隐藏页面指示器点?

编辑:这是我要求的代码。

import UIKit

class TutorialController: UIPageViewController {

    let pageControl = UIPageControl.appearanceWhenContainedInInstancesOfClasses([])

    var currentview = "0"

    private func stylePageControl() {

        pageControl.currentPageIndicatorTintColor = UIColor.lightGrayColor()
        pageControl.pageIndicatorTintColor = UIColor.darkGrayColor()
        pageControl.backgroundColor = UIColor.whiteColor()
    }

    private func hidePageControl() { // this should let us hide the dots on the fourth view, by changing color.

        pageControl.backgroundColor = UIColor(red: 0.2980392157, green: 0.2980392157, blue: 0.2980392157, alpha: 1.0)
        pageControl.currentPageIndicatorTintColor = UIColor(red: 0.2980392157, green: 0.2980392157, blue: 0.2980392157, alpha: 1.0)
        pageControl.pageIndicatorTintColor = UIColor(red: 0.2980392157, green: 0.2980392157, blue: 0.2980392157, alpha: 1.0)
    }


    private(set) lazy var orderedViewControllers: [UIViewController] = {
        return [self.newViewController("1"),
                self.newViewController("2"),
                self.newViewController("3"),
                self.newViewController("4")]
    }()

    private func newViewController(number: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil) .
            instantiateViewControllerWithIdentifier("Tutorial\(number)") // calls the next view controller.
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        stylePageControl() // just changing the color, this works.
        dataSource = self

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

    }
}

// MARK: UIPageViewControllerDataSource

extension TutorialController: UIPageViewControllerDataSource {

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

    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        guard let firstViewController = viewControllers?.first,
            firstViewControllerIndex = orderedViewControllers.indexOf(firstViewController) else {
                return 0
        }

        return firstViewControllerIndex
    }


    func pageViewController(pageViewController: UIPageViewController,
                            viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.indexOf(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,
                            viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
            return nil
        }

        let nextIndex = viewControllerIndex + 1
        currentview = "\(nextIndex)"
        print("now on tutorial slide \(currentview)")
        if currentview == "4" { // this is called successfully when the fourth page comes into view.
            print("we are now on the fourth slide; hiding the page dots...") // this successfully prints.


      UIPageControl.appearanceWhenContainedInInstancesOfClasses([TutorialController.s‌​elf]).hidden = true // this should hide the all page controller dots, but it just doesn't work.
        pageControl.hidden = true // also should hide page controller dots, also doesn't work.
        hidePageControl() // this should change the color of the page controller dots, but doesn't (note that this function works to change color when I call it on viewDidLoad, but that's not where I want it; it doesn't work here).

        print(pageControl.hidden) // always prints false, even though I am trying to set it to true.

        }

        let orderedViewControllersCount = orderedViewControllers.count

        guard orderedViewControllersCount != nextIndex else {
            return nil
        }

        guard orderedViewControllersCount > nextIndex else {
            return nil
        }

        return orderedViewControllers[nextIndex]
    }

}

最佳答案

使用 UIPageViewControllerDelegate 方法:

let pageControl = UIPageControl()

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if (index == numberOfPages()-1){
          pageControl.hidden = true
    }else{
          pageControl.hidden = false
    }
}

关于ios - 在 UIPageViewController swift 的最后一页隐藏点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36391401/

相关文章:

swift - 自定义 UIView 类在初始化时总是崩溃

javascript - 将 NSTaggedPointerString 转换为字符串

swift - swift 划分 double 时精度损失

ios - UICollectionViewCell 未填充屏幕宽度

swift - 如何快速解开可选值?

ios - swift 从此代码中删除可选项

swift - 在 Swift 中重构代码

ios - 当我快速打开新 View 时出错

ios - 与 Dropbox Core API 同步

ios - Swift 3 URLSession.shared() 对成员 'dataTask(with :completionHandler:) error (bug)) 的模糊引用