我做了一个PageViewController
来切换不同的View控制器,我想显示我们看到的页面。我添加了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
}
}
有谁知道我应该用什么代码来设置当前页面?提前谢谢!
答案 0 :(得分:4)
我在以下功能中添加了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
func做了同样的事情!
答案 1 :(得分:0)
设置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)
答案 2 :(得分:0)
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
}