Ios 滚动期间崩溃,因为边界返回为零
我的应用程序因以下消息而崩溃: “CALayerInvalidGeometry”,原因:“CALayer位置包含NaN:[NaN-NaN]” 这是因为我的缩放比例计算为无穷大(除以零) 我正在尝试不使用故事板,所以一切都是按程序进行的。我有两个Ios 滚动期间崩溃,因为边界返回为零,ios,swift,uiscrollview,Ios,Swift,Uiscrollview,我的应用程序因以下消息而崩溃: “CALayerInvalidGeometry”,原因:“CALayer位置包含NaN:[NaN-NaN]” 这是因为我的缩放比例计算为无穷大(除以零) 我正在尝试不使用故事板,所以一切都是按程序进行的。我有两个ViewController的。源ViewController将新的ViewController推送到堆栈上,如下所示: .. let destinationVC = DetailViewController() destinationVC.setWit
ViewController的。源ViewController
将新的ViewController
推送到堆栈上,如下所示:
..
let destinationVC = DetailViewController()
destinationVC.setWith(img: photo)
navigationController?.pushViewController(destinationVC, animated: true)
...
现在,当我在滚动视图中缩小时,我的目标VC
崩溃。这是因为在updateMiniZoomScaleForSize
中,边界将返回为0。我尝试在其他几个地方调用此函数:
viewdilayoutsubviews
-仍然崩溃
viewWillLayoutSubviews
-仍然崩溃
视图将出现
-仍然崩溃
viewdide出现
-没有崩溃,但图像“跳转”到位
我还尝试在setWith(img:UIImage)
方法中调用image.setNeedsDisplay()
和image.setNeedsLayout
,但我仍然观察到相同的结果
这是我的destinationVC
private let image : UIImageView = {
let img = UIImageView()
img.translatesAutoresizingMaskIntoConstraints = false
img.contentMode = .scaleAspectFit
return img
}()
private lazy var scrollView : UIScrollView! = {
let scroll = UIScrollView(frame: view.bounds)
scroll.contentSize = image.bounds.size
scroll.translatesAutoresizingMaskIntoConstraints = false
return scroll
}()
func setWith(img: UIImage) {
self.image.image = img
image.setNeedsDisplay()
image.setNeedsLayout()
updateMinZoomScaleForSize(view.bounds.size)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
/// Add scrollview
view.addSubview(scrollView)
scrollView.addSubview(image)
scrollView.delegate = self
addScrollViewConstraints()
addImageConstraints()
}
private func addScrollViewConstraints() {
let margins = view.safeAreaLayoutGuide
scrollView.leftAnchor.constraint(equalTo: margins.leftAnchor).isActive = true
scrollView.rightAnchor.constraint(equalTo: margins.rightAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
}
private func addImageConstraints() {
image.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
image.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
image.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
image.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return image
}
private func updateMinZoomScaleForSize(_ size: CGSize) {
print(image.bounds.width)
let widthScale = size.width / image.bounds.width
let heightScale = size.height / image.bounds.height
let minScale = min(widthScale, heightScale)
scrollView.minimumZoomScale = minScale
scrollView.zoomScale = minScale
}
设置UIImageView
的.image
属性不会更改UIImageView
的帧
尝试将setWith()
函数更改为:
func setWith(img: UIImage) {
self.image.image = img
image.frame.size = img.size
updateMinZoomScaleForSize(view.bounds.size)
}
也欢迎任何反馈的方法,所以如果这是一个糟糕的设计,这就是为什么我发现自己在这个职位上,请让我知道!