如何在 UIScrollView 中使 UIImage 可滚动?
How to make a UIImage Scrollable inside a UIScrollView?
我目前有一个 ScrollView
,在 ScrollView
里面我有一个 UIImageView
。
我从我的服务器获得了不同尺寸的图像,而且大多数图像都比我屏幕上的 bounds
大。
如果图片比我的屏幕大,我想滚动浏览图片。
像这样....
这是我目前尝试过的方法。
let image = UIImage(named: "myImg")
let heightInPoints = image?.size.height
let heightInPixels = heightInPoints ?? 0 * image!.scale
let widthInPoints = image?.size.width
let widthInPixels = widthInPoints ?? 0 * image!.scale
self.imageView.frame = CGRect(x: 0, y: 0, width: widthInPixels, height: heightInPixels) //= image
self.imageView.image = image
scrollView.contentSize = CGSize(width: imageView.frame.width, height: imageView.frame.height)
但是好像不行。它要么只垂直或水平滚动,要么从不滚动。
我做错了什么。或者有没有更好的方法来获得这种效果?
解决方案 1.更新框架
首先,确保正确设置视图层次结构(imageView
添加到 scrollView
):
scrollView.addSubview(imageView)
然后我建议将此代码重写为:
let image = UIImage(named: "myImg")
imageView.image = image // setup image to imageView
imageView.frame.size = image?.size ?? .zero // setup image size or zero to imageView
scrollView.contentSize = image.size ?? .zero // setup image size or zero as a content size
解决方案 2. 使用约束
还有另一种使用约束而不是手动设置尺寸的解决方案。这需要更多的代码,但不需要在更改图像时重新计算尺寸。
scrollView.addSubview(imageView) // Setup the views hierarchy
imageView.translatesAutoresizingMaskIntoConstraints = false // Make sure constraints won't interfere with autoresizing mask
NSLayoutConstraint.activate(
[
imageView.leftAnchor.constraint(equalTo: scrollView.leftAnchor), // attaching to the left
imageView.topAnchor.constraint(equalTo: scrollView.topAnchor), // attaching to the top
imageView.rightAnchor.constraint(equalTo: scrollView.rightAnchor), // attaching to the right
imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor) // attaching to the bottom
]
)
然后我们用这样的新图像替换现有图像:
imageView.image = UIImage(named: "myImg")
这两种方法在 Playground 中对我都适用。因此,如果这些仍然不适合您,请分享有关如何设置滚动视图的层次结构和其他参数的更多信息。
我目前有一个 ScrollView
,在 ScrollView
里面我有一个 UIImageView
。
我从我的服务器获得了不同尺寸的图像,而且大多数图像都比我屏幕上的 bounds
大。
如果图片比我的屏幕大,我想滚动浏览图片。
像这样....
这是我目前尝试过的方法。
let image = UIImage(named: "myImg")
let heightInPoints = image?.size.height
let heightInPixels = heightInPoints ?? 0 * image!.scale
let widthInPoints = image?.size.width
let widthInPixels = widthInPoints ?? 0 * image!.scale
self.imageView.frame = CGRect(x: 0, y: 0, width: widthInPixels, height: heightInPixels) //= image
self.imageView.image = image
scrollView.contentSize = CGSize(width: imageView.frame.width, height: imageView.frame.height)
但是好像不行。它要么只垂直或水平滚动,要么从不滚动。
我做错了什么。或者有没有更好的方法来获得这种效果?
解决方案 1.更新框架
首先,确保正确设置视图层次结构(imageView
添加到 scrollView
):
scrollView.addSubview(imageView)
然后我建议将此代码重写为:
let image = UIImage(named: "myImg")
imageView.image = image // setup image to imageView
imageView.frame.size = image?.size ?? .zero // setup image size or zero to imageView
scrollView.contentSize = image.size ?? .zero // setup image size or zero as a content size
解决方案 2. 使用约束
还有另一种使用约束而不是手动设置尺寸的解决方案。这需要更多的代码,但不需要在更改图像时重新计算尺寸。
scrollView.addSubview(imageView) // Setup the views hierarchy
imageView.translatesAutoresizingMaskIntoConstraints = false // Make sure constraints won't interfere with autoresizing mask
NSLayoutConstraint.activate(
[
imageView.leftAnchor.constraint(equalTo: scrollView.leftAnchor), // attaching to the left
imageView.topAnchor.constraint(equalTo: scrollView.topAnchor), // attaching to the top
imageView.rightAnchor.constraint(equalTo: scrollView.rightAnchor), // attaching to the right
imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor) // attaching to the bottom
]
)
然后我们用这样的新图像替换现有图像:
imageView.image = UIImage(named: "myImg")
这两种方法在 Playground 中对我都适用。因此,如果这些仍然不适合您,请分享有关如何设置滚动视图的层次结构和其他参数的更多信息。