func setScrollViewImage() { for i in 0..<3 { let imageView = UIImageView() imageView.image = imageArrary[i] imageView.contentMode = .scaleAspectFill let xPosition = self.horizontalScrollView.frame.width * CGFloat(i) let leftSpace = (self.horizontalScrollView.frame.width - (self.horizontalScrollView.frame.width * 2/3)) / 2 imageView.frame = CGRect(x: xPosition + leftSpace, y: 0, width: self.horizontalScrollView.frame.width * 2/3, height: self.horizontalScrollView.frame.height) horizontalScrollView.contentSize.width = self.view.frame.width * CGFloat(1+i) horizontalScrollView.addSubview(imageView) } }