如何让我的图像填充整个ScrollView?

时间:2018-05-13 14:47:18

标签: ios swift uiscrollview uiimageview

我有一系列图像,我想制作水平图像滑块。但是图像并不总是适合整个滚动视图。这是我的代码:

scrollView.delegate = self

scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false

for (index, imageView) in imagesArray.enumerated() {
    imageView.frame.size.height = scrollView.frame.size.height
    imageView.frame.size.width = scrollView.frame.size.width
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    scrollView.addSubview(imageView)
    imageView.frame.origin.x = CGFloat(index) * self.view.bounds.size.width
}
scrollView.contentSize.width = self.view.frame.width * CGFloat(imagesArray.count)

我想让图像填满下图中黑色边框区域内的整个区域。

enter image description here

3 个答案:

答案 0 :(得分:0)

您必须设置contentSize

for (index, imageView) in imagesArray.enumerated()
{
    imageView.frame.size.height = scrollView.frame.size.height
    imageView.frame.size.width = self.view.frame.size.width
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    scrollView.addSubview(imageView)
    imageView.frame.origin.x = CGFloat(index) * self.view.bounds.size.width
}


scrollView.contentSize = CGSize.init(width: scrollView.frame.size.width * imagesArray.count, height: scrollView.frame.size.height)

在此处查看演示scrollDemo

答案 1 :(得分:0)

我认为你只需修改一行

imageView.contentMode = .scaleAspectFill

imageView.contentMode = .scaleAspectFit

答案 2 :(得分:0)

这可能比你想要的更多,但Jarrod的这段视频应该解释你想要的效果:https://www.youtube.com/watch?v=IUT0WByY7HI