У меня есть UIPageViewController
с 2 UIViewControllers
, которые оба имеют UIImageView
в качестве фона с режимом содержимого, установленным на scaleAspectFill
.По какой-то причине это приводит к тому, что второй UIViewController
частично виден при прокрутке с первого по второй UIViewController
, а затем с прокруткой назад к первому UIViewController
.
. Проект воспроизведения можно найти здесь: https://github.com/Jasperav/PageViewControllerGlitch. Код также под фотографией.
Фото (пробел - первый UIViewController
, зеленая часть - вторая UIViewController
):
Код (я не использую раскадровки, для пути воспроизведения с правильным окном используйте ссылку github):
import UIKit
class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
private let pageViewControllers: [UIViewController] = [VC1(), VC2()]
init() {
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
dataSource = self
delegate = self
setViewControllers([pageViewControllers.first!], direction: .forward, animated: false, completion: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return pageViewControllers.count
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let index = pageViewControllers.index(of: viewController), index > 0 else { return nil }
let previousIndex = index - 1
return pageViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let index = pageViewControllers.index(of: viewController), (index + 1) != pageViewControllers.count else { return nil }
return pageViewControllers[index + 1]
}
}
class VC: UIViewController {
init(background: UIImage) {
super.init(nibName: nil, bundle: nil)
let backgroundImageView = UIImageView(image: background)
backgroundImageView.contentMode = .scaleAspectFill
view.addSubview(backgroundImageView)
backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
backgroundImageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
backgroundImageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
backgroundImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
backgroundImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class VC1: VC {
init() {
super.init(background: #imageLiteral(resourceName: "bg1"))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class VC2: VC {
init() {
super.init(background: #imageLiteral(resourceName: "bg2"))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}