以没有中间间距的方式设置 UICollectionViewCell 的大小 [英] Setting size of UICollectionViewCell in a way that there is no interim spacing in between

查看:17
本文介绍了以没有中间间距的方式设置 UICollectionViewCell 的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UICollectionView,我想在其上并排放置七个单元格.应该为此使用整个屏幕.目前,我正在使用集合视图的宽度并将其除以七.现在我在 iPhone 4 上获得了 45.71429 的项目宽度.在一些单元格之间有一个临时间距.我该如何处理?我想填满整个屏幕,所有项目都应该有相同的大小.

I'm using an UICollectionView on which I want to place seven cells side by side. The whole screen should be used for this. Currently, I'm using the width of the collection view and divide it by seven. Now I get an item width of 45.71429 on an iPhone 4. Between some cells there is an interim spacing. How can I handle this? I want to fill out the whole screen and all items should have the same size.

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

One option which comes to my mind is to round the value and use the remaining value as inset. But isn't there a better way?

推荐答案

试试这个!

最重要的是添加 UICollectionViewDelegateFlowLayoutUICollectionViewDelegate 到你的控制器

Most importantly add UICollectionViewDelegateFlowLayout and UICollectionViewDelegate to your controller

为您的单元格大小实现以下 insetsizeForItem 方法.

Implement below methods for inset and sizeForItem for your cell size.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(0, 0, 0, 0)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    // Here, I need 3 equal cells occupying whole screen width so i divided it by 3.0. You can use as per your need.
    return CGSize(width: UIScreen.main.bounds.size.width/3.0, height: UIScreen.main.bounds.size.width/3.0)                
}

对于单元格之间的 0 间距,我们需要像这样指定它的布局.

For 0 spacing between cells, we need to specify it’s layout like this.

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
self.collectionView!.collectionViewLayout = layout

谢谢.希望这会有所帮助!

Thanks. Hope this helps!

这篇关于以没有中间间距的方式设置 UICollectionViewCell 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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