UICollectionView 3 列网格,1px 空间?(包括图片) [英] UICollectionView 3 column grid, 1px space?? (image included)

查看:16
本文介绍了UICollectionView 3 列网格,1px 空间?(包括图片)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个带有 FlowLayout 的 UICollectionView,它在垂直或水平方向的单元格之间没有空间.

I have setup a UICollectionView with a FlowLayout that leaves no space in between cells in either the vertical or horizontal directions.

我一切正常,但在第 2 列和第 3 列之间有一个奇怪的 1px 空间,我不知道为什么!?我已经验证了 iOS 模拟器和真实设备上都出现了 1px 的差距.有人经历过吗?

I have everything working, yet there is an odd 1px space between the 2nd and 3rd column and I have no idea why!? I have verified the 1px gap shows up both in iOS simulator and on a real device. Has anybody experienced this?

我的 UIViewController 是以下的委托/数据源:

My UIViewController is a delegate/datasource of the following:

MyViewController 类:UIViewController、UICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout

我已经实现了删除任何间距的必要功能(并通过它们正在运行的打印语句进行验证),以及对彼此相邻排列的单元格的视觉确认:

I have implemented the necessary functions to remove any spacing (and verified with print statements that they are running), as well as visual confirmation of the cells lining up next to each other:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsZero
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 0
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 0
}

最后,我打印出要返回的单元格的宽度(每个颜色方块)以验证它是 (view.frame.width/3) 还是 (320/3),即106.666667

Finally, I have printed out the width of the cell being returned (each color square) to verify it was the (view.frame.width / 3) or (320/3), which is 106.666667

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    var totalHeight: CGFloat = (self.view.frame.width / 3)
    var totalWidth: CGFloat = (self.view.frame.width / 3)

    println(totalWidth) // this prints 106.666666667

    return CGSizeMake(totalWidth, totalHeight)
}

推荐答案

在类似情况下最好避免 number 的 double 类型.使用:

Better avoid number's double type in similar situations. Use:

return CGSizeMake(ceil(totalWidth), ceil(totalHeight))

这篇关于UICollectionView 3 列网格,1px 空间?(包括图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆