Swift UIScrollView - 奇怪的填充

时间:2015-01-21 14:23:14

标签: ios swift uiscrollview

我需要让花朵图像翻转。图像必须具有相同的高度,但宽度要自动设置。我希望他们左右滚动 这是我的代码:

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!

    var images = [UIImage]()

    override func viewDidLoad() {
        super.viewDidLoad()


        scrollView.delegate = self

        for i in 1...3 {
            images.append(UIImage(named: "bild-0\(i).jpg")!)
        }
        var i: CGFloat = 0
        var origin: CGFloat = 0
        let height: CGFloat = scrollView.bounds.height

        for image in images {
            let imageView = UIImageView(frame: CGRectZero)
            imageView.frame.size.height = height
            imageView.image = image
            imageView.sizeToFit()
            imageView.frame.origin.x = origin
            println(imageView.frame.size.width)
            println(imageView.frame.origin.x)
            println(imageView.frame.size.height)
            println("asd")
            origin = origin + imageView.frame.size.width
            i++
            scrollView.addSubview(imageView)
        }

        scrollView.contentSize.width = origin

        scrollView.bounces = false
        scrollView.pagingEnabled = false

    }
}

故事板:

storyboard

问题(从顶部填充! - 红色 - 是UIScrollView的背景): problem

图像为765x510 300x510,依此类推 UIScrollView高度为170

1 个答案:

答案 0 :(得分:4)

这是由滚动插入引起的:

单击Storyboard上的ViewController并转到文件检查器,您应该看到此对话框:

Adjust ScrollView Insets

取消调整滚动视图插图。