ios - 以中间没有中间间距的方式设置 UICollectionViewCell 的大小

标签 ios uicollectionview uicollectionviewcell uicollectionviewlayout spacing

我正在使用 UICollectionView,我想在上面并排放置七个单元格。为此应使用整个屏幕。目前,我正在使用 Collection View 的宽度并将其除以七。现在,我在 iPhone 4 上得到一个 45.71429 的项目宽度。在一些单元格之间有一个中间间距。我该如何处理?我想填满整个屏幕,所有项目都应该具有相同的大小。

我想到的一个选择是将值四舍五入并使用剩余值作为插图。但是有没有更好的方法呢?

最佳答案

第 1 步:实现 UICollectionViewDelegateFlowLayout(如果未完成)。

第 2 步:使用 UICollectionViewDelegateFlowLayout 的委托(delegate)方法。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSizeMake((UIScreen.mainScreen().bounds.width)/7,120); //use height whatever you wants.
}

第 3 步:转到您拥有 CollectionView 的 XIB 或 StoryBoard

第 4 步:在您拥有 CollectionView 的 XIB 或 StoryBoard 中,点击 CollectionView。

第 5 步:转到 InterfaceBuilder,然后在倒数第二个选项卡(即:Size Inspector)中设置 Min Spacing

对于单元格 = 0

对于行 = 0

那个。

关于ios - 以中间没有中间间距的方式设置 UICollectionViewCell 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28898088/

相关文章:

ios - 在 iOS Google Analytics 中跟踪 "Back"按钮事件

ios - 根据 uiLabel 文本更改水平 Collection View 宽度

ios - (Swift) UICollectionViewCell 带有手势识别器的嵌套 subview

ios - 启用分页的 UICollectionView 中心单元格

ios - ios返回键只有一个文本字段被关闭

ios - 如何在iOS中获取具体日期?

ios - 现代 iOS 8 NSOperation 使用仍然需要@autoreleasepool 吗?

ios - 如何获取位于 UICollectionView 中心的单元格的 indexPath

ios - UICollectionView 没有通过 cellForItemAt 正确更新所有单元格

ios - 调整 UICollectionViewCell 以适合内容