我目前正在尝试将页面指示器(PageControl
)添加到我的UIPageViewController
中。浏览网络时,我发现通过添加presentationCount
和presentationIndex
,指示符将自动出现。
要更改颜色,我尝试过:
let color = UIColor(red: 24/255, green: 90/255, blue: 189/255, alpha: 1)
let appearance = UIPageControl.appearance(whenContainedInInstancesOf: [UIPageViewController.self])
appearance.pageIndicatorTintColor = UIColor.groupTableViewBackground
appearance.currentPageIndicatorTintColor = color
一切正常,但是以某种方式使视图向上移动到指示器上方,并在圆点后面显示黑色背景(可能是因为视图正在移动)。
完整代码:
import UIKit
class LeasingTutorialViewController: UIPageViewController, UIPageViewControllerDataSource {
var pageControl = UIPageControl()
var scrollView = UIScrollView()
lazy var viewControllerList:[UIViewController] = {
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc1 = sb.instantiateViewController(withIdentifier: "leasingPageOne")
let vc2 = sb.instantiateViewController(withIdentifier: "leasingPageTwo")
return [vc1, vc2]
}()
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
changeIndicators()
if let firstViewController = viewControllerList.first as? LeasingPageOneViewController {
self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
}
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return viewControllerList.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
func changeIndicators() {
let color = UIColor(red: 24/255, green: 90/255, blue: 189/255, alpha: 1)
let appearance = UIPageControl.appearance(whenContainedInInstancesOf: [UIPageViewController.self])
appearance.pageIndicatorTintColor = UIColor.groupTableViewBackground
appearance.currentPageIndicatorTintColor = color
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }
let previousIndex = vcIndex - 1
guard previousIndex >= 0 else { return nil }
guard viewControllerList.count > previousIndex else { return nil }
return viewControllerList[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }
let nextIndex = vcIndex + 1
guard viewControllerList.count != nextIndex else { return nil }
guard viewControllerList.count > nextIndex else { return nil }
return viewControllerList[nextIndex]
}
}