【发布时间】:2019-12-23 12:59:40
【问题描述】:
我有一个 UICollectionView,每个 UICollectionViewCell 都是一个带有 UIImageView 的自定义类
@interface MyCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *image;
@end
然后我有一个我想放入 imageview 的图像。但我无法让图像正确地适合图像视图。这是图像。 这些是确切的尺寸,我认为这是 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。
【问题讨论】:
-
你对图像视图设置了哪些限制?
-
我的图像视图设置为适合 UIICollectionViewCell 我将更新我的答案以显示它的图像。这就是你所说的约束吗?
-
看起来您可能没有添加任何约束。尝试在图像视图和单元格之间添加顶部、前导、底部和尾随约束,以便在单元格更改大小时正确调整图像视图的大小 - 否则即使单元格是不同的大小。您还肯定需要将图像视图的
Content Mode(在您的故事板图像中看到)从Scale to Fill更改为Aspect Fit,以使图像在不拉伸的情况下缩放。尝试进行这些更改,然后让我知道它的作用。 -
哇!是的,这有效。我按照您所说的设置了约束并将内容模式设置为 Aspect Fit,现在它可以正确显示了。谢谢!
标签: ios objective-c iosdeployment