My hierarchy is scrollView
> UiView
(named it contentView) > imageView
.
I have the sv pinned to the l,r,t,b of the parent view, the contentView pinned to the t,b of the sv and the width of the parent view, and the imageView
pinned to the l,r,t,b of the contentView.
When using the iPad the image takes up the entire screen. When I swipe up and down the scrollView scrolls with no issues. When I swipe left or right I get a crash:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
Why can I scroll up and down with no issues but get a crash when trying to scroll left or right?
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)
}
}
Update:
I tried using these constraints and it still crashed:
containerView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
containerView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true