我遇到这个问题,我的PageViewController中的点出现在两个点之间的点。当我保存页面并加载它们时,页面会显示正常的点,适当的点数和正常位置。这是我正在谈论的图像。
层次结构调试器显示此图像。显然,这些点出现了多次,彼此相继。
以下是我用于点的代码:
func configurePageControl() {
pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))
self.pageControl.numberOfPages = storyPageViewControllers.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.black
self.pageControl.alpha = 0.5
self.pageControl.pageIndicatorTintColor = UIColor.white
self.pageControl.currentPageIndicatorTintColor = UIColor.black
self.view.addSubview(pageControl)
}
在viewDidAppear中调用configurePageControl()。我尝试将它移动到viewDidLoad,但它只显示了一个点,并且在制作新页面时没有显示更多。
如果你想看看我的页面是如何创建的,那么这是我对UIPageViewController类的扩展:
extension BookPageViewController: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = storyPageViewControllers.index(of: viewController as! TemplateViewController) else { return nil }
if viewControllerIndex == 0 { return nil } // Page won't scroll below first page.
let previousIndex = viewControllerIndex - 1
guard storyPageViewControllers.count > previousIndex else { return nil }
currentPage = Double(previousIndex)
return storyPageViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = storyPageViewControllers.index(of: viewController as! TemplateViewController) else { return nil }
let nextIndex = viewControllerIndex + 1
let storyPageViewControllersCount = storyPageViewControllers.count
guard storyPageViewControllersCount != nextIndex else { return nil }
guard storyPageViewControllersCount > nextIndex else { return nil }
currentPage = Double(nextIndex)
return storyPageViewControllers[nextIndex]
}
// MARK: Delegate function
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
let pageContentViewController = pageViewController.viewControllers![0]
self.pageControl.currentPage = storyPageViewControllers.index(of: pageContentViewController as! TemplateViewController)!
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return storyPageViewControllers.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
guard let firstViewController = viewControllers?.first,
let firstViewControllerIndex = storyPageViewControllers.index(of: firstViewController as! TemplateViewController) else
{ return 0 }
return firstViewControllerIndex
}
}
答案 0 :(得分:2)
1-绝对不要在viewDidappear上调用configurePageControl()。是错的,可以给你错误和奇怪的用户体验。
2- U r调用configurePageControl()3次。你偶然在pageViewController上有3个viewController吗?
解决方案:
A:我会在Storyboard中创建pageViewController,只需在完成所有ViewControllers设置后更新点数。
B:保持所有原样但是:
1-将configurePageControl()移动到ViewDidLoad()
2-保持这样,初始点为0
(提示1:在没有必要时避免使用“自我”)
(提示2:避免使用框架,使用约束)
func configurePageControl() {
pageControl = UIPageControl(frame: CGRect(x: 0,y:
UIScreen.main.bounds.maxY - 50,width:
UIScreen.main.bounds.width,height: 50))
pageControl.numberOfPages = 0
pageControl.currentPage = 0
pageControl.tintColor = UIColor.black
pageControl.alpha = 0.5
pageControl.pageIndicatorTintColor = UIColor.white
pageControl.currentPageIndicatorTintColor = UIColor.black
view.addSubview(pageControl)
}
3-在完成所有viewControllers的设置后,更新pageViewControllers点数
pageControl.numberOfPages = storyPageViewControllers.count
请注意,如果放在这里,它应该有效,但不是正确的地方。你可以做它只是为了测试。
func presentationCountForPageViewController(pageViewController:
UIPageViewController) -> Int {
pageControl.numberOfPages = storyPageViewControllers.count
return storyPageViewControllers.count
}
答案 1 :(得分:2)
UIPageViewController已经拥有页面控件;你所要做的就是展示它(通过实现这两个数据源方法)。您不应该向其添加另一个页面控件。包含的页面控件已经与页面视图控制器同步。你做的工作远比你应该做的多得多。