Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPageViewController in combination with safe area

I can not get UIPageViewController to work in combination with Safe Area as I would expect it to do.

What I am doing is I pin a subview of a UIPageViewControllers child view controller (e.g. childVc1) to the safe area (childVc1.view.safeAreaLayoutGuide). This works fine until I switch orientation from portrait to landscape, swipe to the next child view controller, rotate back to portrait and go back to the previous child view controller. Then the view of the previous child view controller is only updated after the scrolling has finished, which looks pretty strange.

Here's a demo using the Page-Based App-Template provided by Xcode (modified so that the content view is pinned to safe area). It does not have the best fps but it should be clear, e.g. the month name changes position after scrolling has finished.

Did someone encounter similar problems and found a way how to properly fix this?

Thanks

like image 405
Sascha Avatar asked Nov 19 '22 01:11

Sascha


1 Answers

I just ran into this too. It seems like (at least as of iOS 13.5) the safeAreaLayoutGuide doesn't get updated for the offscreen pages until after pageViewController(_:didFinishAnimating:previousViewControllers:transitionCompleted:)

I was able to workaround this by adding the following to the my UIPageViewController subclass, it works by forcing the page view controller to remove the offscreen pages after rotation by "resetting" the visible view controllers.

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
        
     coordinator.animate(alongsideTransition: nil) { (context) in
        self.setViewControllers(self.viewControllers, direction: .forward, animated: false, completion: nil)
    }
}
like image 110
ikono Avatar answered Jan 20 '23 12:01

ikono