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.
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.
Subclass the UIPageViewController
and in viewDidLoad()
call self.view.backgroundColor = .white
. This should fix your problem.