ios - TableView 单元格中的 ScrollView 将无法正确显示图像,并且 swift 4 中的分页模式下有一页额外的页面

标签 ios uiscrollview uiimageview swift4 scroll-paging

我在表格 View 单元格中使用了带有图像的 ScrollView ,但是有两个问题: 1-有一个额外的页面不是我的图像数量并且 2:图像没有填满屏幕,并且每个图像之间有空白 here is the image of what I see

这是我的表格 View 单元格,带有 ScrollView 代码

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
}
}

最佳答案

你能试试这个吗

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()
  }
}

关于ios - TableView 单元格中的 ScrollView 将无法正确显示图像,并且 swift 4 中的分页模式下有一页额外的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49335617/

相关文章:

ios - 从 ScrollView 连续滚动到嵌套 Collection View

ios - UICollectionViewCell 内的 UIScrollView 不滚动

ios - UIScrollView 中的 UIImageView 无法正确缩放

ios - 绘制动画比对图像数组进行动画处理更有效吗?

ios动画。嵌套动画如何影响持续时间?

ios - 设置导航 Controller 上方和 popupView 下方的 View

ios - RxSwift——MainScheduler.instance 与 MainScheduler.asyncInstance

ios - 如何在 ScrollView Swift 中为图像变化设置动画

swift - 根据来自服务器的图像调整imageview大小,然后调整其他标签

ios - 如何跳过最顶层 View 的手势 - iOS