lavenderblue
lavenderblue

Reputation: 95

Black background behind controllers in UIPageViewController

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.

Upvotes: 1

Views: 666

Answers (2)

malhal
malhal

Reputation: 30549

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;

Upvotes: 0

Victor Pro
Victor Pro

Reputation: 269

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

Upvotes: 1

Related Questions