在UICollectionViewFlowLayout中,从Storyboard设置单元格itemSize?

时间:2014-09-28 13:20:50

标签: ios uicollectionview uicollectionviewlayout

... UICollectionViewFlowLayout

@interface MediumBounce : UICollectionViewFlowLayout
@property (nonatomic, strong) UIDynamicAnimator *animator;
etc

@implementation MediumBounce

-(void)prepareLayout
    {
    [super prepareLayout];
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.minimumInteritemSpacing = 0;
    self.minimumLineSpacing = 0;
    self.itemSize = CGSizeMake(92,144);
    self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    {
    if(self.animator)
    etc etc etc

您必须设置 .itemSize ...

所以我有很多集合视图,单元格大小为92.144。但是在另一个集合视图中,单元格大小为200.144。

对于那个,我只是复制类,命名新类MediumBounce200,并更改将 .itemSize 设置为200.144的代码行。

有更好的方法吗?你能以某种方式从故事板中读取大小吗?

或者也许来自collectionVC,你能“向下”并设定它吗?当α

(请注意,您在故事板中创建单元格的大小,我很确定,实际上并不重要:大小仅由FlowLayout中的上述代码设置。)

1 个答案:

答案 0 :(得分:0)

问题Can you read the size from the storyboard somehow?

有一种方法。

  1. 可能在viewController中选择storyboard中的集合视图。
  2. 在身份检查器中设置视图的运行时属性 - > User Defined Runtime Attributes