lundi 29 juin 2015

Using UIScrollView to cycle through images

Storyboard view of controller

I have an imageView inside of a scrollView, and I want to be able to upload up to 3 images and have the user swipe between them. I have accomplished this simply by replacing the contents of the view with the information I want, but it doesn't look or feel as good as a scroll view with pagination would.

I have all of the code to make each image appear when I want it, but my problem is with the scrollview constraints and how to add more content to a scrollview with fixed width. No matter what I do, I can't seem to extend the scroll range when I add an image to the view.

here is what I'm trying now:

let newImageView:UIImageView = UIImageView()
self.imageScrollView.contentSize = CGSize(width: self.imageView.frame.size.width * 3, height: self.imageView.frame.size.height)
self.imageScrollView.pagingEnabled = true

for i = 0; i < 3; ++i {
    var xOrigin:CGFloat = CGFloat(i) * self.view.frame.size.width
    newImageView.frame = CGRectMake(self.imageView.image!.size.width * i, 0, self.imageView.image!.size.width, self.imageScrollView.frame.size.height)
    newImageView.image = images[i]
    newImageView.contentMode = UIViewContentMode.ScaleAspectFit
    self.imageScrollView.addSubview(newImageView)
}

With this code I am able to add the images, but I cannot scroll to them. In my storyboard, the scrollview constraints look like this:

enter image description here

I'm sure some of these constraints are redundant, but I can't figure out why the content width won't extend and allow me to scroll through all of the images in the scrollview.

Aucun commentaire:

Enregistrer un commentaire