I used scroll view with images in table view cell But there are two problems : 1- there is an extra page not the count of my images and 2: the image doesn't fill the screen and there are white space between each image
and here is my table view cell with scroll view codes
class scrollViewCell: UITableViewCell , UIScrollViewDelegate {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
var contentWidth : CGFloat = 0.0
override func awakeFromNib() {
super.awakeFromNib()
scrollView.delegate = self
for image in 0...2 {
let imageToDisplay = UIImage(named : "1")
let imageView = UIImageView(image : imageToDisplay)
let xCoordinate = self.contentView.frame.midX + self.contentView.frame.width * CGFloat(image)
contentWidth += self.contentView.frame.width
scrollView.addSubview(imageView)
imageView.frame = CGRect(x: xCoordinate - 50 , y: 0 , width: self.contentView.frame.width, height: 100)
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
imageView.contentMode = .scaleAspectFit // OR .scaleAspectFill
imageView.clipsToBounds = true
}
scrollView.contentSize = CGSize(width: contentWidth, height: self.contentView.frame.height)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
pageControl.currentPage = Int(scrollView.contentOffset.x / CGFloat(414))
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Can you try this
func addscrollV()
{
for image in 0...2 {
let imageToDisplay = UIImage(named : "1")
let imageView = UIImageView(image : imageToDisplay)
let xCoordinate = self.contentView.frame.width * CGFloat(image)
contentWidth += self.contentView.frame.width
scrollView.addSubview(imageView)
imageView.frame = CGRect(x: xCoordinate , y: 0 , width: self.contentView.frame.width, height: self.contentView.frame.height)
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
}
scrollView.contentSize = CGSize(width: contentWidth, height: self.contentView.frame.height)
}
func layoutSubviews()
{
super.layoutSubviews()
if(Once)
{
Once = false
self.addscrollV()
}
}