Creating an image slider using UIScrollView in Swift

I am making an application that a user can upload from 1 to 3 photos. I want the user to be able to see them as a page control slider, I started to encode it, but for some reason the images are not added to scrollView (I don’t see anything when the application starts).

This is my code:

    class dogProfileViewController: UIViewController {
    var imagesArray = [UIImageView()]
    @IBOutlet var scrollView: UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let image1: UIImageView = UIImageView(image: UIImage(named: "image1.png"))
        let image2: UIImageView = UIImageView(image: UIImage(named: "image2.png"))
        imagesArray.append(image1)
        imagesArray.append(image2)
        var countIndex = CGFloat(0)
        for image in imagesArray {
            image.frame = CGRectMake(countIndex*view.frame.width, 0, view.frame.width, 100)
            scrollView.addSubview(image)
            countIndex = countIndex + 1
        }

        // Do any additional setup after loading the view.
    }
}
+4
source share
2 answers

Add the following line after the for loop ends

scrollView.contentSize = CGSizeMake(countIndex*view.frame.width,0)

I think this will work.

+2
source

, , , scrollView . , , , scrollView . self.view.addSubiew(scrollView). scrollView, , .

, , scrollView. ScrollView , . , scrollView.

+1

Source: https://habr.com/ru/post/1650580/


All Articles