ios - 尝试向左或向右滚动屏幕时 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
解决方案
我更新了你的一些代码
我将本地图片添加00
到imageView
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 对其进行了测试,并且可以正常工作。希望对你有帮助
推荐阅读
- angular - 访问 *ngFor 内部的表单组控件
- c++ - 将 pt::ptree::value_type 用于大量 json 矩阵时的 SIGSEGV
- html - 调用 (CKEditor) 函数 insertElement() 删除标签内容
- c# - 计算网络驱动器上已用磁盘空间
- vim - 检查地图是否存在于 vimScript 中
- python-3.x - Python3 从循环中中断
- r - 函数 pcf.ppp() 与 spatstat 中的 pcfinhom() 有何不同?
- xamarin - 为 Xamarin 创建 Android 绑定时出现问题
- python - 使用 pyTrends 时,出现以下错误:TypeError: __init__() missing 2 required positional arguments: 'google_username' and 'google_password'
- java - 当我使用 LocationManager.getLastKnowLocation() 时,应用程序在真实设备中的第二次启动时崩溃