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

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

问题描述

这是一个黄色的简单UICollectionView

Here's a simple UICollectionView in yellow

红色箭头设置单元格的宽度。 (TBC:单击粉红色单元格:对于'size'选择'Default',然后您在红色箭头处设置的内容将成为单元格的大小。)

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的宽度设置单元格宽度 / strong>?

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换句话说,正如尼基塔指出的那样,

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

推荐答案

我想你需要看看at

- (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天全站免登陆