首页 > 解决方案 > 尝试向左或向右滚动屏幕时 UIScrollView 崩溃

问题描述

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

我将 sv 固定到父视图的 l,r,t,b,将 contentView 固定到 sv 的 t,b 和父视图的宽度,并imageView固定到 l,r,t,b的内容视图。

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

* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: 尝试从对象 [0] 插入 nil 对象”

为什么我可以毫无问题地向上和向下滚动,但在尝试向左或向右滚动时会崩溃?

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

标签: iosswiftuiviewuiscrollview

解决方案


我更新了你的一些代码

我将本地图片添加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 模拟器的 gif。

在此处输入图像描述

我已经用 iPhone 对其进行了测试,并且可以正常工作。希望对你有帮助


推荐阅读