UICollectionView,只是让单元格适合宽度? [英] UICollectionView, simply fit cell to width?

查看:27
本文介绍了UICollectionView,只是让单元格适合宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的黄色 UICollectionView

Here's a simple UICollectionView in yellow

红色箭头设置单元格的宽度.(待定:点击粉色单元格:对于大小"选择默认",然后您在红色箭头处设置的内容将成为单元格的大小.)

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.)

例如,直立 iPhone 设置宽度为 320.

Example, for an upright iPhone set width to 320.

但这似乎很疯狂......我当然可以根据 UICollectionView 的宽度设置单元格宽度?

But this seems crazy ... surely I can set the cell width based on the width of the UICollectionView?

自动调整视图本身没有问题..

There's no problem autosizing the view itself ..

效果很好.

但我无法将 CELL 的宽度设置为与视图相同",这似乎很疯狂.似乎很难相信必须在代码中手动设置?

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?

TBC 换句话说,正如 Nikita 指出的那样,

TBC In other words, as Nikita points out,

-(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?!

推荐答案

我觉得你需要看看

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

UICollectionViewLayout,您可以在其中根据方向和当前帧设置大小.

of UICollectionViewLayout where you can set size based on orientation and current frame.

看起来您确实只需要在代码中执行此操作.

It would indeed seem that you simply have to do this in code.

这篇关于UICollectionView,只是让单元格适合宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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