swiftpaginationimageviewscrollview

Center ImageView in ScrollView with paging enabled - Swift


I need to create a paging ScrollView which shows a sequence of images.

I created a ScrollView in the main view of the Storyboard and set this constraints (to center the ScrollView in the view):

Constraint

Then I activated paging and disabled the "Content layout guides" option.

Next, in the view class I set up the UIScrollViewDelegate delegate and I wrote the following code to show 3 images (they are 3 colored squares):

class ViewController: UIViewController, UIScrollViewDelegate {

// Outlet
@IBOutlet weak var scrollview: UIScrollView!


override func viewDidLoad() {
    super.viewDidLoad()

    scrollview.delegate = self;

    let infoArray = ["01", "02", "03"];

    for i in 0..<infoArray.count {
        let imageView = UIImageView();
        imageView.contentMode = .scaleToFill;
        imageView.image = UIImage(named: infoArray[i]);
        let xPos = CGFloat(i) * scrollview.bounds.size.width;
        imageView.frame = CGRect(x: xPos, y: 0, width: scrollview.bounds.size.width, height: scrollview.bounds.size.height);
        imageView.layer.borderWidth = 1;

        scrollview.contentSize.width = scrollview.frame.size.width * CGFloat(i+1);
        scrollview.contentSize.height = scrollview.frame.size.height;

        scrollview.addSubview(imageView);
    }

    scrollview.layer.borderWidth = 1;
}

}

I have set that the images must have the same width and height as the scrollview. But these are larger in the simulator (and in my iPhone 11) and therefore the display is incorrect. I show you the sequence of the 3 squares:

Page 1

Page 2

Page 3

Page 4

I can't understand where I'm wrong. Why don't the 3 images take the size of the scrollview? Why are there 4 pages?

Thanks for your help


Solution

  • Okay, here is how you do it:

    1. Your scrollview is created in storyboard and its layout is set. Make sure content layout guides is unchecked in the size inspector and paging is checked in the attribute inspector.

    2. Add a stackview as a subview to your scrollview (this will act as the content view). Pin your stackView to all 4 edges of the scrollView.

    3. Set Height and Width Equal to the scrollView height and width. Set the Width priority to 250. (that indicates that the scrollview will scroll horizontally)

    4. Set the stackView to horizontal axis, fill alignment and fillEqually distribution.

    5. Now, go back to viewDidLoad and add the following code below. ScrollViewContentView is the stackView that acts as a contentView for the scrollView. Note that since stackView is set to fillEqually, you only need set one of the image's width constraint.

      scrollViewContentView.addArrangedSubview(image1)
      scrollViewContentView.addArrangedSubview(image2)
      scrollViewContentView.addArrangedSubview(image3)
      
      image1.translatesAutoresizingMaskIntoConstraints = false
      image2.translatesAutoresizingMaskIntoConstraints = false
      image3.translatesAutoresizingMaskIntoConstraints = false
      
      image1.backgroundColor = .blue
      image2.backgroundColor = .yellow
      image3.backgroundColor = .red
      
      image1.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true