Вопрос

I am trying to implement a side scrolling UICollectionView with custom UICollectionViewCells. Each of the custom cells contains a UIImageView that should fill the cell. Right now I am setting the size of the UIImageView like this,

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellID = @"cellID";
THCVCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; 
cell.imageView.image = [self.images objectAtIndex:(indexPath.row % [self.images count])];
cell.imageView.frame = cell.bounds;

return cell;  
}

but the UIImageView does not fill the cell until the cell is dequeued for reuse.

What should I change to make the UIImageView appear as it should when the cells are first shown?

Это было полезно?

Решение

What if Inside of THCVCell.m, you added something like this:

- (void) layoutSubviews {
    _imageView.frame = self.contentView.bounds;
}

That way, if the cell ever changes size, the imageView will adapt.

Then remove:

cell.imageView.frame = cell.bounds;

Другие советы

Try the following, which will make the imageView resize as the cell resizes.

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellID = @"cellID";
THCVCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; 
cell.imageView.image = [self.images objectAtIndex:(indexPath.row % [self.images count])];
cell.imageView.frame = cell.contentView.bounds;
cell.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

return cell;  
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top