一切正常,但点不显示。我的代码是...
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
}
}
答案 0 :(得分:1)
您使用的是这些功能的错误版本。
为了显示UIPageControl,您需要实现两个可选的数据源方法。只需返回 presentationCount 的全部页面数和 presentationIndex
最初选择的索引func presentationCount(for pageViewController: UIPageViewController) -> Int {
setupPageControl()
return orderedViewControllers.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
答案 1 :(得分:1)
您应该添加页面视图控制器数据源和页数:
pageControl.numberOfPages = 3