繁体   English   中英

UICollectionView具有零像元间距

[英]UICollectionView with zero cell spacing

我遵循了这个答案,并尝试实现每行5个单元,当我按以下方式检查iPhone 6和iPhone SE时,它的工作效果很好。

但是,当我尝试在iPhone 6 Plus上运行它时,就会出现问题。 有人可以帮我解决问题吗?

这是我的代码。

screenSize = UIScreen.main.bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let itemWidth : CGFloat = (screenWidth / 5)

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.sectionInset = UIEdgeInsets(top: 10.0, left: 0, bottom: 10.0, right: 0)
collectionView!.collectionViewLayout = layout

它一定要是

let itemWidth : CGFloat = (screenWidth / 5.0)

因此结果将不会四舍五入。

更新

请确保如果使用情节提要板创建UICollectionView,请记住将自动布局设置为集合视图的大小,以便将其更新为当前的屏幕大小。

更新2

如果使用情节UICollectionViewFlowLayout ,则无需创建UICollectionViewFlowLayout 您可以设置情节提要的插图和间距。 然后在您的.m文件中执行此操作以确定项目的大小。

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
     return yourDesiredSize;
}

要固定空格,每个单元格的大小应为整数。 然后,四舍五入的数字之和与collectionView的大小之间的差异可以平均分配或放在一个单元格中。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    // To avoid white space inbetween cells we're rounding the width of each cell
    var width = CGFloat(floorf(Float(screenWidth/CGFloat(objects.count))))
    if indexPath.row == 0 {
        // Because we're rounding the width of each cell the cells don't cover the space completly, so we're making the first cell a few pixels wider to make sure we fill everything
        width = screenWidth - CGFloat(objects.count-1)*width
    }
    return CGSize(width: width, height: collectionView.bounds.height)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM