UIPageControl 显示当前页面

UIPageControl to show current page

我做了一个 PageViewController 来在不同的视图控制器之间切换,我想显示我们看到的页面。我添加了一个pageControl,并给了它下面的代码。

pageControl.currentPageIndicatorTintColor = UIColor.blueColor()
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.numberOfPages = pages.count

完整代码:

import UIKit

class ViewControllerSportinfrastructuur: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

@IBOutlet weak var pageControl: UIPageControl!

var pageViewController: UIPageViewController!
let pages = ["PageOneViewController", "PageTwoViewController"]

//MARK: - page view controller data source
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

    if let index = pages.indexOf(viewController.restorationIdentifier!) {
        if index > 0 {
            return viewControllerAtIndex(index - 1)
        }
    }

    return nil
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    if let index = pages.indexOf(viewController.restorationIdentifier!) {
        if index < pages.count - 1 {
            return viewControllerAtIndex(index + 1)
        }
    }
    return nil
}

func viewControllerAtIndex(index: Int) -> UIViewController? {
    let vc = storyboard?.instantiateViewControllerWithIdentifier(pages[index])
    return vc
}

override func viewDidLoad() {
    super.viewDidLoad()

    if let vc = storyboard?.instantiateViewControllerWithIdentifier("MyPageViewController") {
        self.addChildViewController(vc)
        self.view.addSubview(vc.view)

        pageViewController = vc as! UIPageViewController
        pageViewController.dataSource = self
        pageViewController.delegate = self
        pageViewController.setViewControllers([viewControllerAtIndex(0)!], direction: .Forward, animated: true, completion: nil)
        pageViewController.didMoveToParentViewController(self)

        pageControl.currentPageIndicatorTintColor = UIColor.blueColor()
        pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
        pageControl.numberOfPages = pages.count
    }
}

有谁知道我应该使用什么代码来设置当前页面?提前致谢!

设置pageViewController.delegate = self,执行协议UIPageViewControllerDelegate

并使用这些方法中的一种来设置 self.pageControl.currentPage = ...

optional func pageViewController(_ pageViewController: UIPageViewController,
 willTransitionToViewControllers pendingViewControllers: [UIViewController])

optional func pageViewController(_ pageViewController: UIPageViewController,
              didFinishAnimating finished: Bool,
         previousViewControllers previousViewControllers: [UIViewController],
             transitionCompleted completed: Bool)

编辑:已经找到答案了!

我在以下函数中添加了pageControl.currentPage = index

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

    if let index = pages.indexOf(viewController.restorationIdentifier!) {
        pageControl.currentPage = index
        if index > 0 {
            return viewControllerAtIndex(index - 1)
        }
    }

    return nil
}

并对 viewControllerAfterViewController 函数也做了同样的事情!

UIPageViewController 提供默认值 UIPageControl - 如果您没有特定的自定义要求,我建议您使用它。

为了显示它,您需要在 UIPageViewControllerDataSource 中实现以下两个可选方法:

func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return n //number of view controllers
}

func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    return i //index of the visible view controller
}