简体   繁体   English

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

[英]Black background behind controllers in UIPageViewController

I have a simple UIPageViewController with 3 pages, which are all ViewControllers embedded in NavigationControllers .我有一个简单的 UIPageViewController,有 3 个页面,它们都是嵌入在NavigationControllers ViewControllers

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已解决:在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.编辑:将视图放在 NavigationControllers 或 PageViewController 后面没有帮助。 The color's always black no matter what the color of PageViewController background is.无论 PageViewController 背景的颜色是什么,颜色总是黑色。

Subclass the UIPageViewController and in viewDidLoad() call self.view.backgroundColor = .white .子类化UIPageViewController并在viewDidLoad()调用self.view.backgroundColor = .white This should fix your problem.这应该可以解决您的问题。

UIPageViewController 's view is transparent so the black you are seeing is the window. UIPageViewController的视图是透明的,所以你看到的黑色是窗口。 Thus just change the windows background color in your app delegate or scene delegate, eg因此,只需更改应用程序委托或场景委托中的窗口背景颜色,例如

self.window.backgroundColor = UIColor.whiteColor;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM