Making Image Slider with UIScrollView in Swift

11,122

Solution 1

Add the following line after the for loop ends

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

I think this will work.

Solution 2

@IBOutlet var mainScrollView: UIScrollView!

var imageArray = [UIImage]()

override func viewDidLoad() {
    super.viewDidLoad()

    mainScrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 175)

    imageArray = [#imageLiteral(resourceName: "cake"),#imageLiteral(resourceName: "food")]

    for i in 0..<imageArray.count{
        let imageView = UIImageView()
        imageView.image = imageArray[i]
        imageView.contentMode = .scaleToFill
        let xPosition = self.view.frame.width * CGFloat(i)
        imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)

        mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
        mainScrollView.addSubview(imageView)
    }
}

Solution 3

Well, according to what you provided, you haven't added the scrollView to the view. So, first step, make sure the scrollView is added to the view. self.view.addSubiew(scrollView). Change the scrollView background to make sure it is there.

Also, I don't see that you have initialized the scrollView frame. The scrollView has no frame, so it won't show anywhere. Make sure that you initialize the scrollView.

Share:
11,122
Eliko
Author by

Eliko

Updated on August 22, 2022

Comments

  • Eliko
    Eliko over 1 year

    I am making an app that the user can upload 1 to 3 photos. I want the user to be able to see them it as a slider in with Page Control, I started to code it but for some reason the images do not added to the scrollView (I see nothing when running the app).

    This is my code so far:

        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.
        }
    }
    
  • Eliko
    Eliko over 7 years
    I added it from the mainstoryboard to the UIViewController
  • impression7vx
    impression7vx over 7 years
    Ahh! I see the outlet now! So what happens when you change the background of the scrollView? Can you see it change colors?
  • Eliko
    Eliko over 7 years
    well when I change the background color it does change but it do not get added when.. (Sorry for the delay :) )