kle*_*vre 4 iphone objective-c ios uicollectionview
我试图将水平集合视图的第一个和最后一个单元格居中。
到目前为止,我发现的唯一方法是在我的UICollectionView. 由于显而易见的原因,这种方法完全是一个坏主意,但我现在没有找到更好的方法。并且因为使用 iPhone 6 和 iPhone 6+,我的视图布局完全被破坏了......(facepalm)
为了说明我的需要,我需要显示如下内容:
| x x x x x| where the first x represent my first cell
and
|x x x x x | where the last x represent my last cell
Run Code Online (Sandbox Code Playgroud)
我认为使用UICollectionViewFlowLayout子类是处理contentInset东西的正确方法,但我对这些组件完全是新手。
关于如何实现这一目标的任何链接或建议?
在这里发布以供将来参考,因为我一直在解决同样的问题,并找到了解决方案。使用 Swift 3 在 Xcode 版本 8.2.1 (8C1002) 中测试
UICollectionViewDelegate 具有 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
要将集合视图中的第一个和最后一个单元格居中,您可以通过以下方式计算插图:
let inset: CGFloat = collectionView.frame.width * 0.5 - cellSize.width * 0.5
Run Code Online (Sandbox Code Playgroud)
在此之后,您只需返回新的 UIEdgeInsetMake(0, inset, 0, inset)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let inset: CGFloat = collectionView.frame.width * 0.5 - cellSize.width * 0.5
return UIEdgeInsetsMake(0, inset, 0, inset)
}
Run Code Online (Sandbox Code Playgroud)
-------编辑-------
在这种情况下,我将单元格大小存储为硬编码值,并使用委托进行设置 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
| 归档时间: |
|
| 查看次数: |
2890 次 |
| 最近记录: |