0

I have a ScrollView combined with a PageControll and it contains 5 images which I want them to scroll. My problem is that the ScrollView width even if it is 320 in simulator it doesn't show covering the all width.

enter image description here

This is my code:

override func viewDidLoad() {
    super.viewDidLoad() 

    images.append(UIImage(named: "one.jpg")!)
    images.append(UIImage(named: "two.jpg")!)
    images.append(UIImage(named: "three.jpg")!)
    images.append(UIImage(named: "four.jpg")!)
    images.append(UIImage(named: "five.jpg")!)

    for var i = 0; i < images.count; i++ {
        var frame: CGRect = CGRectMake(0, 0, 0, 0)
        frame.origin.x = self.scrollView.frame.size.width * CGFloat(i)
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        var imageView: UIImageView = UIImageView(frame: frame)
        imageView.image = images[i]
        self.scrollView.addSubview(imageView)
    }

    self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(images.count), scrollView.frame.size.height)
}
Adrian
  • 19,440
  • 34
  • 112
  • 219

4 Answers4

1

Most likely, the scroll view's width is the correct size, but the content-mode of the UIImageView is set incorrectly such that as a result of the image being displayed having a smaller size than the scroll view, it will not fill the whole of the image view as you wanted.

imageView.contentMode = UIViewContentModeScaleAspectFill;

// Swift

imageView.contentMode = .ScaleAspectFit
max_
  • 24,076
  • 39
  • 122
  • 211
0

Try adding constraints to your image in your interface builder.

0

Put Constraints: Main.storyboard -> UIImageView (for each UIImageView) -> Editor -> Pin -> Select leading,top space, bottom and trailing space to superview.

0

Your scrollView doesn't have constraints

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84