UIPageViewController 中控制器后面的黑色背景

Black background behind controllers in UIPageViewController

我有一个简单的 UIPageViewController,有 3 个页面,它们都是 ViewControllers 嵌入在 NavigationControllers 中。

当我滚动浏览这些页面时,右边的页面在整个滚动过程中都有黑色背景。当控制器卡入到位时,它会恢复正常。

Here's how it looks.

我该如何解决这个问题?

已解决:在 UIPageViewController 的子类中:self.view.backgroundColor = .white

代码:

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]
}

编辑:将视图放在 NavigationControllers 或 PageViewController 后面没有帮助。无论 PageViewController 背景是什么颜色,颜色始终是黑色。

子类 UIPageViewController 并在 viewDidLoad() 中调用 self.view.backgroundColor = .white。这应该可以解决您的问题。

UIPageViewController 的视图是透明的,所以您看到的黑色是 window。因此只需更改应用委托或场景委托中的 windows 背景颜色,例如

self.window.backgroundColor = UIColor.whiteColor;