Search code examples
swiftuipageviewcontroller

UIPageViewControllers UIPageControl glitches during transition


I have three Controllers that shall be shown via UIPageViewController. It works just fine, but the UIPageControl does flicker during the transition. What do I miss/dont't see here?

I will present you my UIPageViewController Implementation, the ViewController Implementation and the code I use to push it. Thank you for your time!

The code I create the UIPageViewController and the controllers it contains:

let blueVC = EmptyViewController()
blueVC.view.backgroundColor = UIColor.blue
blueVC.title = "blauer Controller"

let greenVC = EmptyViewController()
greenVC.view.backgroundColor = UIColor.green
greenVC.title = "grüner Controller"

let redVC = EmptyViewController()
redVC.view.backgroundColor = UIColor.red
redVC.title = "roter Controller"

let subViewControllers = [blueVC, greenVC, redVC]

let pageViewController = DemoPageViewController(withControllers: subViewControllers)

self.navigationController?.pushViewController(pageViewController, animated: true)

PageViewController:

import UIKit

class DemoPageViewController: UIPageViewController {
    private var myViewControllers = [UIViewController]()

    public init(withControllers controllers: [UIViewController]) {
        self.myViewControllers = controllers
        super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        self.setViewControllers([controllers.first!], direction: .forward, animated: true, completion: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.backgroundColor = UIColor.clear
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self
        // Do any additional setup after loading the view.
    }
}

extension DemoPageViewController: UIPageViewControllerDataSource {
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = self.myViewControllers.index(of: viewController) else {
            return nil
        }

        let previousIndex = viewControllerIndex - 1

        guard previousIndex >= 0 else {
            return self.myViewControllers.last
        }

        guard self.myViewControllers.count > previousIndex else {
            return nil
        }

        return self.myViewControllers[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = self.myViewControllers.index(of: viewController) else {
            return nil
        }

        let nextIndex = viewControllerIndex + 1

        guard nextIndex < self.myViewControllers.count else {
            return self.myViewControllers.first
        }

        guard self.myViewControllers.count > nextIndex else {
            return nil
        }

        return self.myViewControllers[nextIndex]
    }

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return self.myViewControllers.count
    }

    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        return 0
    }
}

Additional the Implementation of the EmptyViewControllers:

import Foundation
import PureLayout
import UIKit

class EmptyViewController: UIViewController {
    var didSetupConstraints = false

    override func loadView() {
        self.view = UIView()
        self.view.backgroundColor = UIColor.white

        self.view.setNeedsUpdateConstraints() // bootstrap Auto Layout
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print("\(#function) in \(#file)")
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func updateViewConstraints() {
        // Check a flag didSetupConstraints before creating constraints, because this method may be called multiple times, and we
        // only want to create these constraints once. Without this check, the same constraints could be added multiple times,
        // which can hurt performance and cause other issues. See Demo 7 (Animation) for an example of code that runs every time.
        if !self.didSetupConstraints {
            // set Constraints here

            self.didSetupConstraints = true
        }

        super.updateViewConstraints()
    }
}

Solution

  • I just found the reason. That flickering was caused by the ViewController in the background that could be seen because the background was clear. Setting the background to black did help.