我有一个 UICollectionView,每个 UICollectionViewCell 都是一个带有 UIImageView 的自定义类
@interface MyCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *image;
@end
然后我有一张我想放在图像视图中的图像。但我无法让图像正确地适合图像视图。这是图像。这些是确切的尺寸,我认为这是 36x36,但本质上我不希望尺寸重要,我只是希望它按比例缩小并适合任何尺寸。故事板中 UIImageView 的尺寸为 59x54 (我知道这可能会根据屏幕显示单元格的方式而改变,因此大小再次不应该相关)这些是故事板中视图的当前显示设置
和我的 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;
}
结果看起来像这样所以你会看到边缘是如何被略微切断的。我确实阅读了其他一些尝试的答案,
[cell.image setContentMode:UIViewContentModeScaleAspectFit];
但这也不起作用。有人对此有解决方案吗?UIImageView 像这样适合 UICollectionViewCell
外蓝线是 UICollectionViewCell,内蓝线是 UIImageView。