如何使这个垂直空间更接近?

时间:2016-11-01 17:19:15

标签: ios swift swift3

问题是,如何让这个空间更近?

enter image description here

这是我的代码:

class ViewController: UIViewController
{
    @IBOutlet var mainScrollView: UIScrollView!
    var imageArray = [UIImage]()

    override func viewDidLoad()
    {
        super.viewDidLoad()

        mainScrollView.frame = view.frame
        imageArray = ["tumblr1","tumblr3","tumblr2","tumblr4","tumblr5"]

        for i in 0..<imageArray.count
        {
            let imageView = UIImageView()
            imageView.image = imageArray[i]
            imageView.contentMode = .scaleAspectFit

            let yPosition = self.view.frame.height * CGFloat(i)
            imageView.frame = CGRect(x: 0, y: yPosition, width: self.view.frame.width, height: self.view.frame.height)
            mainScrollView.contentSize.height = mainScrollView.frame.height * CGFloat(i + 1)
            mainScrollView.addSubview(imageView)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你的问题是:

let yPosition = self.view.frame.height * CGFloat(i)

尝试制作yPosition:

let yPosition = previousImage.frame.origin.y+previousImage.frame.size.height

在移动到下一张图像之前,只需跟踪上一张图像。同时将imageView的高度设置为图像的高度,而不是视图。

您可能希望将for循环更改为:

 var yPosition = 0

for i in 0..<imageArray.count
{
    let imageView = UIImageView()
    imageView.image = imageArray[i]
    imageView.contentMode = .scaleAspectFit

    imageView.frame = CGRect(x: 0, y: yPosition, width: self.view.frame.width, height: self.view.frame.height)
    mainScrollView.addSubview(imageView)

    yPosition += imageView.image?.size.height
    mainScrollView.contentSize.height = yPosition
}