我有一个包含 UIScrollView
的 ViewController,它将前导、尾随和底部对齐到安全区域,将顶部对齐到 super View 。它只包含一张图像,该图像应该尽可能大,保持宽高比,适合屏幕并且可缩放。我对其施加了以下限制:
将中心 x 和 y 与 super View 对齐
宽度/高度 ≤ super View 的宽度/高度
保持纵横比
但现在图像开始具有原始尺寸(小于 ipad 的宽度和高度),并且根据我放大的位置,我无法滚动到每个位置。我怎样才能做到这一点? 这是我的 View Controller :
class FullscreenImageViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var fullImageView: UIImageView!
@IBOutlet weak var closeButton: UIButton!
var imageSource: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
fullImageView.image = imageSource
}
/**
* Function used to close the presented ViewController.
* @param sender The tapped button.
*/
@IBAction func closeFullscreenViewController(_ sender: UIButton) {
self.dismiss(animated: false, completion: nil)
}
/**
* Function used to handle the zooming functionality. The ScrollView is given the view it should use for the zooming, in this case the imageView.
* @param scrollView The ScrollView.
*/
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return fullImageView
}
}
最佳答案
I put the following constraints on it: align center x and y to superview width/height ≤ width/height of superview keep aspect ratio
这就是问题所在。您没有做任何事情来使 ScrollView 可滚动。
默认情况下, ScrollView 是不可滚动的;您需要设置其内容大小,以便它知道滚动多少。您的 ScrollView 的内容大小为零,因此它不可滚动。它不知道图像的缩放尺寸,因此当您缩放图像时,您仍然无法滚动它。
您需要将 ImageView 的边缘固定到 ScrollView ,以便当 ImageView 展开时, ScrollView 知道其边缘在哪里,并让您看到它们。
关于swift - 无法滚动到可缩放图像中的每个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57112597/