Refresh UIPageViewController - reorder pages and add new pages

后端 未结 4 1692
悲&欢浪女
悲&欢浪女 2020-11-30 21:27

I have a UIPageViewController which I am providing page data for using an implementation of UIPageControllerDelegate and UIPageControllerData

相关标签:
4条回答
  • 2020-11-30 22:02

    I found a workaround to force UIPageViewController to forget about cached view controllers of neighboring pages that are currently not displayed:

    pageViewController.dataSource = nil;
    pageViewController.dataSource = self;
    

    I do this everytime I change the set of pages. Of course this doesn't affect the currently displayed page.

    With this workaround I avoid the caching bug and can still use animated:YES in setViewControllers:direction:animated:completion:.

    0 讨论(0)
  • 2020-11-30 22:02

    Swift 4 Version for reload of data of pageviewcontroller, to @theory's version.

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if !completed { return }
        DispatchQueue.main.async() {
            self.dataSource = nil
            self.dataSource = self
        }
    }
    
    0 讨论(0)
  • 2020-11-30 22:07

    Here's my complete implementation of @ortwin-gentz's answer in the didFinish delegate method in Swift 2, which works perfectly for me:

    func pageViewController(
        pageViewController: UIPageViewController,
        didFinishAnimating finished: Bool,
        previousViewControllers: [UIViewController],
        transitionCompleted completed: Bool
    ) {
        if !completed { return }
        dispatch_async(dispatch_get_main_queue()) {
            pageViewController.dataSource = nil
            pageViewController.dataSource = self
        }
    }
    
    0 讨论(0)
  • 2020-11-30 22:08

    You need to call setViewControllers:direction:animated:completion:.

    Also, in iOS 6, watch out if you're using UIPageViewControllerTransitionStyleScroll style, as there is a major caching bug if animated: is YES (see my discussion here: UIPageViewController navigates to wrong page with Scroll transition style).

    0 讨论(0)
提交回复
热议问题