简体   繁体   English

UICollectionView,只是适合单元格宽度?

[英]UICollectionView, simply fit cell to width?

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.) (TBC:单击粉红色单元格:对于“大小”选择“默认”,然后您在红色箭头处设置的内容将变为单元格的大小。)

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". 但似乎很疯狂,我无法将CELL的宽度设置为“与视图相同”。 It seems hard to believe one has to set it manually, in code? 似乎很难相信人们必须在代码中手动设置它?

TBC In other words, as Nikita points out, TBC换句话说,正如尼基塔指出的那样,

-(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中,没有办法在自动布局或其他任何事情上做到这一点?!

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. 看起来你只需要在代码中执行此操作。

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位,我想要8个侧边距,最后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. 如果您继承UICollectionViewLayout或UICollectionViewFlowLayout,您仍然可以覆盖单个单元格,如其他答案中所述。 But if you simply want to have more table view like behavior this should work. 但是如果你只是希望有更多的表视图,就像行为一样,这应该有用。

I my case cell inset making extra shapes 我的情况是细胞嵌入制作额外的形状 在此输入图像描述

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

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