UICollectionViewCell图像内的UIImageView无法正确容纳

时间:2019-12-23 12:59:40

标签: ios objective-c iosdeployment

我有一个UICollectionView,每个UICollectionViewCell是一个带有UIImageView的自定义类

@interface MyCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *image;
@end

然后我有一张要放入imageview的图像。但是我无法使图像正确地放入图像视图中。这是图片。 enter image description here这些是确切的尺寸,我认为这是36x36的尺寸,但本质上我不希望尺寸有问题,我只想缩小尺寸并适合尺寸即可。情节提要中UIImageView的尺寸为59x54(我知道这可以根据屏幕显示单元格的方式而改变,因此大小也应不相关)这是情节提要enter image description here中视图的当前显示设置还有我的UICollectionView委托代码

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    [cell.image setImage:[UIImage imageNamed:@"avocado"]];

    return cell;
}

结果看起来像这样enter image description here,因此您将看到边缘是如何被略微切除的。我确实读过尝试[cell.image setContentMode:UIViewContentModeScaleAspectFit];的其他答案,但是那也不起作用。有人对此有解决方案吗? UIImageView像enter image description here

一样适合UICollectionViewCell内部。

外部蓝线是UICollectionViewCell,内部蓝线是UIImageView。

1 个答案:

答案 0 :(得分:1)

请确保在图像视图和单元格之间添加顶部,顶部,底部和尾部约束,以便在单元格更改大小时正确调整图像视图的大小-否则,即使在情节提要中,图像视图也将保持此大小如果单元格更改大小。

您还肯定需要将图像视图的Content Mode(在情节提要图像中看到)从Scale to Fill更改为Aspect Fit,以便在不拉伸的情况下缩放图像。