在iOS7上,UICollectionViewCell中的UIImageView自动布局存在问题,但在iOS8上没有问题。

3
在ImageView中缩小图像大小遇到问题。在CollectionViewCell中,ImageView具有约束条件:两个水平间隔和两个垂直间隔。
第一个屏幕是iOS7,第二个屏幕是iOS8。 enter image description here enter image description here ImageURL 是一个用于通过URL加载图像的自定义类,它工作正常,并设置了图像。
    _clothesImageView.image = [UIImage imageNamed:@"imageName.png"];

    - (void)configCellWithClothesModel:(ClothesModel *)clothesModel
    {
        [_clothesImageView setImageURL:[NSURL URLWithString:clothesModel.imageURL]];
    }

衣服模型:
- (instancetype)initWithDict:(NSDictionary *)dict
{
    self = [super init];
    if (self)
    {
        _isShop = @(YES);
        self.title = [self checkNullAndNill:dict[kTitle]];
        self.info = [self checkNullAndNill:dict[kDescription_Short]];
        self.artNumber = [self checkNullAndNill:dict[kArtNumber]];
        self.afLink = [self checkNullAndNill:dict[kDeeplink1]];
        self.imageURL = [self checkNullAndNill:dict[kImg_url]];
        _isCart = @(NO);
    }
    return self;
}


//==============================================================================


- (id)checkNullAndNill:(id)object
{
    if (object == nil)
        return @"";
    else if (object == [NSNull null])
        return @"";
    else
        return object;
}


//==============================================================================


- (UICollectionViewCell *)configCellWithType:(NSString *)type collectionView:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath
{
    ClothesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[type isEqualToString:@"Outfits"] ? @"CellOutfits" : @"Cell" forIndexPath:indexPath];
    [cell configCellWithClothesModel:self];
    return cell;
}


//==============================================================================

请给我们提供ClothesModel类的一些代码,特别是图像部分。 - hris.to
放一些代码。你是在 iPhone 5 还是 iPhone 6 上运行代码? - Ashish Kakkad
@hris.to完成。自定义类正常工作。 - Iraklii
@AshishKakkad iPhone 6不支持iOS7。我在所有安装了iOS7的设备上运行,但发现同样的问题。 - Iraklii
我认为可以尝试使用 [imgView.layer setMaskToBound:YES],如果不起作用,则尝试使用 [imgView setContentMode:]。在 contentMode 中,有 UIViewContentModeScaleAspectFill、UIViewContentModeScaleAspectFit 和 UIViewContentModeScaleToFill 等多种选项,但其中的一个可能会有所帮助。 - Mubin Mall
@TheMall 没有帮助。 [_clothesImageView setContentMode:UIViewContentModeScaleAspectFill]; [_clothesImageView.layer setMasksToBounds:YES]; [_clothesImageView setImageURL:url]; - Iraklii
2个回答

7

在Xcode6中,界面构建器不会显示单元格的内容视图。 CollectionViewCell在iOS7中具有内容视图,并且其框架设置为默认值(0,0,50,50)。 您需要在CollectionViewCell子类中添加此代码,以便contentView可以自行调整大小。

- (void)awakeFromNib
{
   [super awakeFromNib];
   self.contentView.frame = self.bounds;
   self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

非常好的答案!帮了我很多。你让我想起了我的奶奶,她在我需要帮助时给了我解决方案! - Misha Akopov

0

这真的取决于Autolayout设置。
UIImageView通常根据其固有内容大小设置其大小,该大小设置为它们承载的图像的大小。
如果您的集合视图单元格没有足够数量的约束条件,则所看到的内容可能取决于不同iOS上的自动布局引擎实现。
还要检查图像视图的内容模式,正如评论中某人所述。


我已经移除了所有的限制,它可以在iOS7上运行,但是当我在iPhone 6或6+上运行时,图像大小没有增加。 - Iraklii
你不需要移除约束,而是要正确设置它们。 - Andrea
例如,假设imageview应该延伸到整个单元格内容视图。imageview的正确约束是leading、trailing、top和bottom,常数等于0。 - Andrea
是的,我做到了。而且是之前完成的。 - Iraklii

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接