Question

I am using SSToolkit/SSCollectionView and I have a custom SSCollectionViewItem but I am having a hard time getting the frame size for that view which was set by the SSCollectionView Delegate:

- (CGSize)collectionView:(SSCollectionView *)aCollectionView itemSizeForSection:(NSUInteger)section {
return CGSizeMake(280.0f, 280.0f);
}

Here is the item being initalizes:

#pragma mark - Initializer

- (id)initWithReuseIdentifier:(NSString *)aReuseIdentifier {
    if ((self = [super initWithStyle:SSCollectionViewItemStyleImage reuseIdentifier:aReuseIdentifier])) {
        if([aReuseIdentifier isEqualToString:@"featuredIdentifier"]) {
            NSLog(@"Frame: %f", self.frame.size.width);
            return self;
        } else {
            self.imageView.backgroundColor = [UIColor colorWithWhite:0.95f alpha:1.0f];
            return self;
        }

        self.imageView.backgroundColor = [UIColor colorWithWhite:0.95f alpha:1.0f];
    }
    return self;
}

NSLog(@"FRAME Width:%f", self.frame.size.width); always returns a 0

Was it helpful?

Solution

This is the expected behavior. All collection view items are always initialized with CGRectZero since they could be reused in a section with different item sizes.

SSCollectionView automatically sizes the items right before they are displayed. You should implement layoutSubviews if you need to do layout based on the item's size.

Also, it looks like you are trying to do different drawing based on the identifier. I highly recommend you make a different class for each identifier to keep things clean and understandable.

OTHER TIPS

Across most (all?) Cocoa frameworks, frames are not set until sometime later, when the view will come on screen. If you are inside a view, this can be as late as willMoveToWindow, I think. In most cases, view geometry should be set by the ViewController during viewWillLayoutSubviews or similar, and subviews can (should) be setup to use proper "struts and springs" (i.e. autoresizingMask). See various parts of the UIView reference page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top