我在表格 View 单元格中使用了带有图像的 ScrollView ,但是有两个问题: 1-有一个额外的页面不是我的图像数量并且 2:图像没有填满屏幕,并且每个图像之间有空白
这是我的表格 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/