Ios 单元间距为零的UICollectionView
我已经遵循并尝试实现每行5个单元,当我检查iphone6和iphonese时,它工作得非常好,如下所示 但当我尝试在iPhone6Plus上运行它时,问题就出现了。有人能帮我解决这个问题吗 这是我的密码Ios 单元间距为零的UICollectionView,ios,iphone,uicollectionview,swift3,uicollectionviewlayout,Ios,Iphone,Uicollectionview,Swift3,Uicollectionviewlayout,我已经遵循并尝试实现每行5个单元,当我检查iphone6和iphonese时,它工作得非常好,如下所示 但当我尝试在iPhone6Plus上运行它时,问题就出现了。有人能帮我解决这个问题吗 这是我的密码 screenSize = UIScreen.main.bounds screenWidth = screenSize.width screenHeight = screenSize.height let itemWidth : CGFloat = (screenWidth / 5) let l
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,请记住将autolayout设置为集合视图的大小,以便将其更新为当前屏幕大小
更新2
如果使用故事板,则无需创建UICollectionViewFlowLayout
。您可以从故事板设置插入和间距。
然后在.m
文件中执行此操作以确定项目的大小
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return yourDesiredSize;
}
一定是这样
let itemWidth : CGFloat = (screenWidth / 5.0)
因此,结果不会四舍五入
已更新
请确保如果使用故事板创建UICollectionView,请记住将autolayout设置为集合视图的大小,以便将其更新为当前屏幕大小
更新2
如果使用故事板,则无需创建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)
}
要修复空白空间,每个单元格的大小应该是一个整数。然后,四舍五入的数字之和与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)
}
看起来这些都是图像。是吗?储物柜#1000?layout.itemSize=CGSize(宽度:itemWidth,高度:itemWidth)这是用来控制物品大小的。尝试将screenWidth/5用于itemWidth@MNM这是一个标签。是的,我试着把
layout.itemSize=CGSize(宽度:screenWidth/5,高度:screenWidth/5)
放进去,但运气不好:(尝试将其放入容器视图并将背景涂成黑色您必须在模拟器上运行它。在设备上尝试,我认为您不会看到这些线条。这些线条看起来像是图像。是吗?储物柜#1000?布局。itemSize=CGSize(宽度:itemWidth,高度:itemWidth)这可以控制您的项目大小。请尝试将screenWidth/5用于itemWidth@MNM这是一个UILabel。是的,我试着把layout.itemSize=CGSize(宽度:screenWidth/5,高度:screenWidth/5)
放进去,但没有运气:(尝试将其放入容器视图并将背景涂成黑色您必须在模拟器上运行它。在设备上尝试,我认为您不会看到这些线条。谢谢,但它没有帮助:(相同的场景。collectionview正在使用故事板,自动布局设置为4个方向。感谢您的时间:)谢谢,但它没有帮助:(相同的场景。collectionview正在使用故事板,并且自动布局设置为4个方向。感谢您的时间:)