我有一個UICollectionView顯示圖像的水平佈局。在每張圖片下,我都會顯示一個標籤。在故事板中,我擴展了單元格的高度,以便標籤將顯示在單元格下方。我還將故事板中UIImageView的高度設置爲比單元格低20pts。但是,無論我做什麼,圖像佔用整個單元格,並且標籤顯示在圖像頂部。我應該在其他地方設置imageview的大小嗎?我發現這個thread,我認爲它會幫助,因爲它基本上是相同的,但該解決方案並沒有幫助我。UIImageView填充整個UICollectionView單元
這裏是我的代碼...
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell"; // string value identifier for cell reuse
ImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.layer.borderWidth = 1.0;
cell.layer.borderColor = [UIColor grayColor].CGColor;
NSString *myPatternString = [self.imageNames objectAtIndex:indexPath.row];
cell.imageView.image = [self.imagesArray objectAtIndex:indexPath.row];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
CGSize labelSize = CGSizeMake(CellWidth, 20);
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.bounds.size.width/2, cell.bounds.size.height-labelSize.height, cell.bounds.size.width, labelSize.height)];
testLabel.text = myPatternString;
[cell.contentView addSubview:testLabel];
return cell;
}
感謝您的建議,但我認爲這對我想要做的事情有點矯枉過正。必須有一種非常簡單的方法來修改我已經有的功能,使imageview不會超過我設置的大小。儘管如此,我會玩弄你的建議。 – SonnyB