I have a pageviewcontroller
, but when I swipe to the second page, it shows the right controller for a split second and then the screen goes white, if I then swipe again it will show up as it should be. It also has 4 pages instead of my intended 3 pages.
Here is a video to demonstrate the problem: https://streamable.com/i1inq
The indexing is as following:
profileController.index = 0
discoverController.index = 1
matchController.index = 2
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
if index == 0
{
index = discoverController.index
return discoverController
}
else if index == 1
{
index = matchController.index
return matchController
}
return nil
}
EDIT: I should also mention that the initial controller is the profileController, and so the index starts at 0 when the app is run.
Updated
You can simply modify your code customized viewcontroller with index
property:
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
let index = (viewController as? MyCustomVCWithIndexForPageViewController).index
if index == 0
{
index = discoverController.index
return discoverController
}
else if index == 1
{
index = matchController.index
return matchController
}
return nil
}
Original
You should not cache parameter related to pageviewcontroller.
UIPageViewController
sometimes preload
your page to have a better performance.
Ex:
You have 3 pages to display.
You are now displaying first page initial.
Then you scroll to second view. viewControllerAfter
called with viewController
of first viewcontroller for requesting second viewcontroller.
And viewControllerAfter
called again with viewController
of second viewcontrollerfor requesting third viewcontroller (preload).
In this case if you cache index, it will lead to unpredictable results.
you didnt provide enough code to detect what is causing the problem. but this is how i usually use UIPageController
class ViewController: UIViewController {
var pages = [UIViewController]()
override func viewDidLoad() {
super.viewDidLoad()
// add pages to the array
// ...
}
}
extension ViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
// find index of viewcontroller in array
if let index = pages.index(of: viewController) {
switch index {
// if index is last item you should return first item
case pages.count - 1:
return pages[0]
default:
// by default return next item in array
return pages[index + 1]
}
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
// find index of viewcontroller in array
if let index = pages.index(of: viewController) {
switch index {
// if index is 0 you should return last item in array
case 0:
return pages[pages.count - 1]
default:
// by default return previous item in array
return pages[index - 1]
}
}
return nil
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With