1

I have a simple UIPageViewController with 3 pages, which are all ViewControllers embedded in NavigationControllers.

When I scroll through these pages, the page on the right has a black background behind it during the entire scroll. When the controller snaps in place, it goes back to normal.

Here's how it looks.

How do I fix this??

SOLVED: in subclass of UIPageViewController: self.view.backgroundColor = .white

Code:

lazy var viewControllerList: [UIViewController] = {
    let sb = UIStoryboard(name: "Main", bundle: nil)

    let yesterdayVC = sb.instantiateViewController(withIdentifier: "Yesterday")
    let todayVC = sb.instantiateViewController(withIdentifier: "Today")
    let tomorrowVC = sb.instantiateViewController(withIdentifier: "Tomorrow")

    return [yesterdayVC, todayVC, tomorrowVC]
}()

override func viewDidLoad() {
    super.viewDidLoad()

    self.dataSource = self
    setViewControllers([viewControllerList[1]], direction: .forward, animated: true, completion: nil)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }

    let previousIndex = vcIndex - 1

    guard previousIndex >= 0 else { return nil }
    guard viewControllerList.count > previousIndex else { return nil }

    return viewControllerList[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }

    let nextIndex = vcIndex + 1

    guard viewControllerList.count > nextIndex else { return nil }

    return viewControllerList[nextIndex]
}

Edit: putting a view behind NavigationControllers or PageViewController doesn't help. The color's always black no matter what the color of PageViewController background is.

  • Possible duplicate of [UIPageViewController black background when sliding more](https://stackoverflow.com/questions/31932977/uipageviewcontroller-black-background-when-sliding-more) – El Tomato Mar 13 '19 at 12:34
  • @ElTomato This doesn't help, the color behind controllers is still black. – lavenderblue Mar 13 '19 at 12:43

2 Answers2

1

Subclass the UIPageViewController and in viewDidLoad() call self.view.backgroundColor = .white. This should fix your problem.

Victor Pro
  • 199
  • 4
0

UIPageViewController's view is transparent so the black you are seeing is the window. Thus just change the windows background color in your app delegate or scene delegate, e.g.

self.window.backgroundColor = UIColor.whiteColor;
malhal
  • 26,330
  • 7
  • 115
  • 133