2014-07-26 101 views
0

我有一個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; 
} 

回答

0

不知道這是否會爲你工作,但我不這樣做將UIView的一個子類,它包含兩個子視圖 - 爲圖像的UIImageView和標籤的UILabel。下面是這個小類的精髓(不要被底部四捨五入打擾;在一些地區,我需要底部四捨五入,在其他地方我不需要)。我只是將此子視圖添加到單元格的contentView中。不知道它是否對你有幫助,但在這裏。順便說一句,imageView是一個類變量,但您可以將其定義爲屬性。

- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl roundBottoms:(BOOL)roundBottoms; 
    { 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height-25.0f)]; 

     if (roundBottoms) 
     { 
      imageView.layer.cornerRadius = 12; 
      imageView.layer.masksToBounds = YES; 
     } 
     else 
     { 
      CAShapeLayer * maskLayer = [CAShapeLayer layer]; 
      maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: imageView.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii: (CGSize){12.0, 12.}].CGPath; 
      imageView.layer.mask = maskLayer; 
     } 

     [imageView setImage:img]; 
     imageView.contentMode = UIViewContentModeScaleAspectFill; // default 

     [self addSubview:imageView]; 

     UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 25.0f, frame.size.width, 25.0f)]; 
     label.text = lbl; 
     labelText = lbl; 
     label.textAlignment = NSTextAlignmentCenter; 
     label.textColor = [UIColor blackColor]; 

     label.adjustsFontSizeToFitWidth = YES; 
     label.minimumScaleFactor = 0.50f; 
     label.backgroundColor = [UIColor clearColor]; 
     self.tag = imageItem; // 101 
     [self addSubview:label]; 
    } 
    return self; 
} 

我有它的另一個版本,我用的,我計算了基於該傳入的總體框架高度的標籤的高度。在這其中,單元尺寸是靜態的,所以標籤的高度可爲好。

+0

感謝您的建議,但我認爲這對我想要做的事情有點矯枉過正。必須有一種非常簡單的方法來修改我已經有的功能,使imageview不會超過我設置的大小。儘管如此,我會玩弄你的建議。 – SonnyB