mazy
mazy

Reputation: 690

Swift UIScrollView - strange padding

I need to make the flowers image flipping. Images must be with the same height, but the width to set automatically. I want them to scroll right and left Here is my code:

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:

storyboard

Problem (Padding from top! - Red color - is a background for UIScrollView): problem

Images are 765x510 300x510 and so on UIScrollView height is 170

Upvotes: 1

Views: 2948

Answers (1)

E-Riddie
E-Riddie

Reputation: 14780

This is caused by scrolling insets:

Click your ViewController on Storyboard and go to file inspector, and you should see this dialog:

Adjust ScrollView Insets

Untick the Adjust Scroll View Insets.

Upvotes: 5

Related Questions