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?
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;
}
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;
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.