【问题标题】:Programmatically turns the page, indicators does not change以编程方式翻页,指示器不变
【发布时间】:2016-10-27 08:53:00
【问题描述】:

我尝试以下方法找到here

extension UIPageViewController {

func goToNextPage(){

    guard let currentViewController = self.viewControllers?.first else { return }

    guard let nextViewController = dataSource?.pageViewController( self, viewControllerAfter: currentViewController ) else { return }

    setViewControllers([nextViewController], direction: .forward, animated: true, completion: nil)

   }
}

它有效,但有一个问题:

以编程方式翻页时,指示器不会移动。似乎它们仅在用户转动时才会移动。滑动页面

这就是执行程序化转向后指标的样子:

相反,它们保持不变

这导致指标显示的层次结构是 [2,0,1] 而不是 [0,1,2]

这是我实现指标的方式:

func presentationCount(for PageViewController:UIPageViewController) -> Int {


    return 3

}

func presentationIndex(for PageViewController:UIPageViewController) -> Int {

    return 0
}

当页面以编程方式翻转时如何使点指示器移动?

【问题讨论】:

    标签: ios swift swift3 uipageviewcontroller programmatically-created


    【解决方案1】:

    很遗憾,您无法更新嵌入在 UIPageViewController 中的 UIPageControl。但是,您可以在 UIPageViewController 中拥有自己的 UIPageControl 以获得完全控制。然后,您可以在更新页面时以编程方式更新 UIPageControl 属性。看看这个article.

    【讨论】:

      【解决方案2】:

      有一个变通方法,获取 UIPageViewController 的子视图,将值设置为 currentPage。

      for subView in self.view.subviews{
           if let pageControl = subView as? UIPageControl,
               pageControl.numberOfPages >  currentIndex{
               pageControl.currentPage = currentIndex
           }
      }
      

      【讨论】:

        【解决方案3】:

        你可以,你需要做的就是在一个变量中维护页面索引,我们称之为currentPageIndex并使用以下方法:

        
        // Part of UIPageViewControllerDelegate 
        
        func presentationIndex(for pageViewController: UIPageViewController) -> Int {
          return currentPageIndex;
        }
        
        // In your Button action, set this variable
        
        @IBAction func nextPage(_ sender: Any) {
           var index = (pageViewController?.viewControllers?.last as! SinglePageViewController).pageIndex
           currentPageIndex = index
        }
        
        

        就是这样!!您的页面指示器现在应该可以工作了。 我遇到了和你一样的情况。

        【讨论】:

          猜你喜欢
          • 2013-02-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-23
          • 2011-11-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多