PageViewController 显示部分其他 viewcontroller
PageViewController shows part of other viewcontroller
我有一个 UIPageViewController
和 2 个 UIViewControllers
,它们都有一个 UIImageView
作为背景,内容模式设置为 scaleAspectFill
。由于某些原因,当首先滚动到第二个 UIViewController
而不是滚动回第一个 UIViewController
.
时,第二个 UIViewController
部分可见
复制项目可以在这里找到:https://github.com/Jasperav/PageViewControllerGlitch。密码也在照片下方。
照片(space第一张UIViewController
,绿色部分是第二张UIViewController
):
代码(我不使用故事板,对于具有正确window的再现路径,使用github link):
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")
}
}
当您使用AspectFill 作为内容模式时,如果图像大于UImageView 的尺寸,图像会溢出。要解决此问题,您可以剪辑其子视图:
backgroundImageView.contentMode = .scaleAspectFill
backgroundImageView.clipsToBounds = true
另一种解决方案是尝试另一种内容模式,如 .scaleToFill
。
希望这有帮助,我刚刚在您的演示项目中尝试了这个解决方案并且都有效。
我有一个 UIPageViewController
和 2 个 UIViewControllers
,它们都有一个 UIImageView
作为背景,内容模式设置为 scaleAspectFill
。由于某些原因,当首先滚动到第二个 UIViewController
而不是滚动回第一个 UIViewController
.
UIViewController
部分可见
复制项目可以在这里找到:https://github.com/Jasperav/PageViewControllerGlitch。密码也在照片下方。
照片(space第一张UIViewController
,绿色部分是第二张UIViewController
):
代码(我不使用故事板,对于具有正确window的再现路径,使用github link):
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")
}
}
当您使用AspectFill 作为内容模式时,如果图像大于UImageView 的尺寸,图像会溢出。要解决此问题,您可以剪辑其子视图:
backgroundImageView.contentMode = .scaleAspectFill
backgroundImageView.clipsToBounds = true
另一种解决方案是尝试另一种内容模式,如 .scaleToFill
。
希望这有帮助,我刚刚在您的演示项目中尝试了这个解决方案并且都有效。