uias / Pageboy

📖 A simple, highly informative page view controller
MIT License
1.99k stars 154 forks source link

[Bug] When isInfiniteScrollEnabled, ViewController disappears intermittently. #265

Open KYHyeon opened 3 years ago

KYHyeon commented 3 years ago

When isInfiniteScrollEnabled is true and the scroll direction changes on the last page, the view controller disappears intermittently. This is easy to reproduce if you have two viewcontrollers.

Here is my code

import UIKit
import Pageboy

class ViewController: PageboyViewController {
    let firstVC: UIViewController = {
        let viewController = UIViewController()
        viewController.view.backgroundColor = .systemOrange
        return viewController
    }()

    let secondVC: UIViewController = {
        let viewController = UIViewController()
        viewController.view.backgroundColor = .systemPurple
        return viewController
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self
        self.isInfiniteScrollEnabled = true
    }
}

extension ViewController: PageboyViewControllerDataSource {
    func numberOfViewControllers(in pageboyViewController: PageboyViewController) -> Int {
        2
    }

    func viewController(for pageboyViewController: PageboyViewController, at index: PageboyViewController.PageIndex) -> UIViewController? {
        switch index {
        case 0:
            return firstVC
        case 1:
            return secondVC
        default:
            fatalError("unknown index!")
        }
    }

    func defaultPage(for pageboyViewController: PageboyViewController) -> PageboyViewController.Page? {
        .first
    }
}
narekam commented 2 years ago

I can reproduce this in our app, where we have 7 view controllers and isInfiniteScrollEnabled is true/false (doesn't matter) - when you go quickly between view controllers back and forth one of the views would disappear eventually. And then, if you rotate the device app would crash with console output like this

*** Assertion failure in -[Pageboy.PatchedPageViewController _flushViewController:animated:], UIPageViewController.m:2110
2022-05-26 13:54:31.247654-0700 MotorTrend[3956:523518] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Don't know about flushed view <UIView: 0x121f6eac0; frame = (0 0; 1194 503.5); alpha = 0; autoresize = W+H; layer = <CALayer: 0x2831f92c0>>'