尝试向左或向右滚动屏幕时 UIScrollView 崩溃

UIScrollView crashing when trying to scroll left or right off screen

我的层次结构是 scrollView > UiView(将其命名为 contentView)> imageView.

我将 sv 固定到父视图的 l、r、t、b,将 contentView 固定到 sv 的 t、b 和父视图的宽度,以及 imageView固定到 contentView 的 l、r、t、b。

当使用 iPad 时,图像会占据整个屏幕。当我上下滑动时,scrollView 滚动没有问题。当我向左或向右滑动时出现崩溃:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'

为什么我可以上下滚动而没有问题,但在尝试向左或向右滚动时却崩溃了?

MyVC: UIViewController, UIScrollViewDelegate {

lazy var scrollView: UIScrollView = {
    let scrollView = UIScrollView()
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.backgroundColor = .white
    return scrollView
}()

lazy var containerView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

lazy var imageView: UIImageView = {
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.contentMode = .scaleAspectFill
    return imageView
}()

override func viewDidLoad() {
    super.viewDidLoad()


    scrollView.delegate = self
    scrollView.minimumZoomScale = 1.0
    scrollView.maximumZoomScale = 6.0

    setAnchors()
}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return imageView
}

func setAnchors() {

    view.addSubview(scrollView)
    scrollView.addSubview(containerView)
    containerView.addSubview(imageView)

    scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true

    containerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    containerView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
    containerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true

    imageView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
    imageView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
    imageView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
    imageView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true

    scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: view.frame.height)
}

}

更新:

我尝试使用这些约束,但它仍然崩溃:

containerView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
containerView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true

我更新了你的一些代码

我加一张本地图片00imageView

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        scrollView.delegate = self
        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 6.0

        imageView.image = UIImage.init(named: "00")
        setAnchors()
    }

    lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.backgroundColor = .white
        return scrollView
    }()

    lazy var containerView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }

    func setAnchors() {

        view.addSubview(scrollView)
        scrollView.addSubview(containerView)
        containerView.addSubview(imageView)

        scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true

        containerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
        containerView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
        containerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        containerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true

        imageView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
        imageView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
        imageView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
        imageView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true

        scrollView.contentSize = CGSize(width: view.bounds.size.width, height: view.frame.height)
    }

这是iPad模拟器的动图。

我已经用 iPhone 测试过它,它可以工作。希望对你有帮助