【问题标题】:UICollectionView Horizontal ScrollUICollectionView 水平滚动
【发布时间】:2016-09-17 11:49:59
【问题描述】:

我尝试做一个UICollectionView 在屏幕上打印 1,5 单元格,并且通过滚动您应该能够显示下一个单元格。 但这是问题所在,我设法显示 1,5 单元格,但无法滚动。 我不知道为什么,当我将布局scrollDirection 属性设置为.Horizontal 时,会创建一个垂直滚动。

这里是布局和UICollectionView初始化:

let screenSize = UIScreen.mainScreen().bounds
let widthImage = (screenSize.width - CGFloat(8)) / 1.5
let heightImage = UIUtils.getHeightAspectRatio(widthImage)
//let sizeCollectionView: CGSize = CGSize(width: widthImage, height: heightImage)

let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: widthImage, height: heightImage)

howTo = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
howTo!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "howToCell")
howTo!.tag = 0
howTo!.delegate = self
howTo!.dataSource = self
self.view.addSubview(howTo!)
howTo!.snp_makeConstraints { (make) -> Void in
  make.top.equalTo(readMoreText.snp_bottom).offset(5)
  make.leading.equalTo(self.view).offset(5)
  make.trailing.equalTo(self.view)
  make.height.equalTo(115)
}
howTo?.backgroundColor = UIColor.whiteColor()
howTo?.contentMode = .ScaleToFill
howTo?.scrollEnabled = true

这是显示单元格时调用的方法:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if collectionView.tag == howTo!.tag {
  let cell = collectionView.dequeueReusableCellWithReuseIdentifier("howToCell", forIndexPath: indexPath)
  if let url = NSURL(string:UIUtils.getImageUrl(self.exercise.getHowTos()[indexPath.row].getImages(), label: Image.mediumWide)) {
    let screenSize = UIScreen.mainScreen().bounds
    let widthImage = (screenSize.width - CGFloat(8)) / 1.5
    let heightImage = UIUtils.getHeightAspectRatio(widthImage)
    let img = UIImageView(frame: CGRect(x: 0, y: 0, width: widthImage, height: heightImage))
    img.af_setImageWithURL(url)
    cell.contentView.addSubview(img)
    cell.backgroundView = cell.contentView
  } else {
    let img = UIImageView(frame: cell.frame)
    img.image = R.image.appIconTemp()
    cell.contentView.addSubview(img)
    cell.backgroundView = cell.contentView
    //cell.backgroundColor = UIColor.blackColor()
  }

如果你知道为什么我不能滚动你会成功的!

谢谢

【问题讨论】:

    标签: ios swift uicollectionview horizontal-scrolling


    【解决方案1】:

    试试这个

     let layout = UICollectionViewFlowLayout()
    
     layout.scrollDirection = .Vertical
    
     let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
    

    只需根据需要更改滚动方向

    【讨论】:

    • 谢谢答案,但我已经尝试过了。这并没有改变任何东西,我无法滚动到下一个单元格,我只能打印第一个和第二个的一半。如果我尝试滚动打印第一个单元格的一半和整个第二个单元格,我昨天花了所有时间试图找到问题,但我没有
    • .horizo​​ntal 救了我!谢谢!!
    • 乐于帮助@NupurSharma
    猜你喜欢
    • 2013-10-18
    • 2014-11-15
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多