【问题标题】:Collection view doesn't clip to the edges of the view when scrolling and dismissing滚动和关闭时,集合视图不会剪辑到视图的边缘
【发布时间】:2020-11-01 06:28:37
【问题描述】:

我有一个UICollectionView,我的问题是,如果我在集合视图滚动仍在进行时关闭包含集合视图的视图控制器,则集合视图不会被剪裁到边缘视图,当视图控制器被关闭时,我可以看到视图边界之外的单元格。

我正在使用集合视图流布局,这是我的代码:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let flow = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    
    let height = collectionView.frame.height - flow.sectionInset.top - flow.sectionInset.bottom
    let size = CGSize(width: cellWidth, height: height)
    return size
}

我也用下面的代码来操作滚动过程,所以每次滚动都会在指定的点停止:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
  
    let width: CGFloat = cellWidth + cellOffset
    let xOffset = scrollView.contentOffset.x
    let page: Double = Double(xOffset / width)
    var pageInt: Int = Int(page.round(to: 0))
    
    if velocity != .zero {
        pageInt = velocity.x > 0 ? pageInt+1 : pageInt-1
    }
    
    let newXOffset = CGFloat(pageInt) * width
    let point = CGPoint(x: newXOffset, y: 0)
    targetContentOffset.pointee = point
}

【问题讨论】:

    标签: ios swift xcode uicollectionview uiscrollview


    【解决方案1】:

    所以我在发布问题后几乎立即发现了我的愚蠢错误。我将阴影应用到我的集合视图层并将其masksToBounds 设置为false。将masksToBounds 设置为true 即可解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 2019-04-06
      • 2017-10-02
      相关资源
      最近更新 更多