简体   繁体   中英

In a UICollectionViewFlowLayout, set cell itemSize from Storyboard?

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

You have to set the .itemSize ...

So I have many collection views, the cell size is 92.144. However on another collection view, cell size is 200.144.

For that one, I just copy the class, name the new class MediumBounce200, and change the line of code that sets the .itemSize to 200.144.

Is there a better way? Can you read the size from the storyboard somehow?

Or perhaps from the collectionVC, can you "reach down" and set that? When?

(Note that the size which you make the cell in the storyboard , I'm pretty sure, actually doesn't matter: the size is set only by the code above in the FlowLayout.)

For question Can you read the size from the storyboard somehow?

There is a way.

  1. Choose the collection view in storyboard probably inside a viewController.
  2. Set the view's runtime attributes in identity inspector -> User Defined Runtime Attributes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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