【问题标题】:UIImageView filling entire UICollectionView cellUIImageView 填充整个 UICollectionView 单元格
【发布时间】:2014-07-26 01:44:20
【问题描述】:

我有一个 UICollectionView 显示图像的水平布局。在每张图片下,我都显示了一个标签。在情节提要中,我扩展了单元格的高度,以便标签显示在单元格下方。我还将情节提要中 UIImageView 的高度设置为比单元格低 20pts。但是,无论我做什么,图像都会占据整个单元格,并且标签会显示在图像顶部。我应该在其他地方设置图像视图的大小吗?我发现了这个thread,我认为它会有所帮助,因为它基本上是相同的,但解决方案对我没有帮助。

这是我的代码...

- (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;
}

【问题讨论】:

    标签: uiimageview uicollectionview


    【解决方案1】:

    不知道这是否适合你,但我这样做是 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;
    }
    

    我有另一个版本,我使用它根据传入的整体框架高度计算标签的高度。在这个版本中,单元格大小是静态的,因此标签高度也可以。

    【讨论】:

    • 感谢您的建议,但我认为这对于我正在尝试做的事情来说有点矫枉过正。必须有一种非常简单的方法来修改我已经必须使 imageview 填充的内容不超过我设置的大小。不过我会考虑你的建议。
    猜你喜欢
    • 1970-01-01
    • 2011-03-14
    • 2014-04-27
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2016-12-31
    相关资源
    最近更新 更多