ios UICollectionView,只是让单元格适应宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24915443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollectionView, simply fit cell to width?
提问by Fattie
Here's a simple UICollectionView in yellow
这是一个简单的黄色 UICollectionView
The red arrow sets the width of the cell. (TBC: clicking on the pink cell: for 'size' select 'Default', then what you set at the red arrow becomes the size of the cell.)
红色箭头设置单元格的宽度。(待定:点击粉红色的单元格:对于“大小”选择“默认”,然后您在红色箭头处设置的内容将成为单元格的大小。)
Example, for an upright iPhone set width to 320.
例如,对于直立的 iPhone,将宽度设置为 320。
But this seems crazy ... surely I can set the cell width based on the width of the UICollectionView?
但这似乎很疯狂......我当然可以根据 UICollectionView 的宽度设置单元格宽度?
There's no problem autosizing the view itself ..
自动调整视图本身没有问题..
That works fine.
这很好用。
But it seems crazy that I can't set the width of the CELL to be "same as the view". It seems hard to believe one has to set it manually, in code?
但是我无法将 CELL 的宽度设置为“与视图相同”似乎很疯狂。似乎很难相信必须在代码中手动设置它?
TBC In other words, as Nikita points out,
TBC 换句话说,正如 Nikita 指出的那样,
-(CGSize) collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return self.view.frame.size;
}
(indeed you also typically need this ...)
(实际上你通常也需要这个......)
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0,0,0,0); //t,l,b,r
}
In fact - you have to do that in code?! There's no way to do that in Storyboard, with autolayout, or anything else?!
事实上 - 你必须在代码中做到这一点?!没有办法在 Storyboard、自动布局或其他任何东西中做到这一点?!
回答by Nikita Took
I think you need to take a look at
我认为你需要看看
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
of UICollectionViewLayout
where you can set size based on orientation and current frame.
的UICollectionViewLayout
,你可以根据方向和当前帧设置大小。
It would indeed seem that you simply have to do this in code.
看起来您确实只需在代码中执行此操作。
回答by Tjp
There is a simple way to do this. Here is the swift code. Assumes you want one item wide and in the example my cells are 64 pts high, and I want 8 pts for side margins, and finally collectionView is the name of your UICollectionView.
有一种简单的方法可以做到这一点。这是快速代码。假设你想要一个项目宽,在这个例子中我的单元格高 64 pts,我想要 8 pts 的边距,最后 collectionView 是你的 UICollectionView 的名称。
Insert this into viewDidLoad :
将其插入 viewDidLoad :
let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = CGSize.init(width: (UIScreen.main.bounds.width - 16), height: 64.0)
This will set all the cells to default to that size. You can still override individual cells if you subclass UICollectionViewLayout or UICollectionViewFlowLayout, as mentioned in other answers. But if you simply want to have more table view like behavior this should work.
这会将所有单元格设置为默认大小。如果您子类化 UICollectionViewLayout 或 UICollectionViewFlowLayout,您仍然可以覆盖单个单元格,如其他答案中所述。但是,如果您只是想拥有更多类似行为的表视图,那么这应该可行。