UICollectionViewCell 图像中的 UIImageView 不适合
UIImageView inside UICollectionViewCell image won't fit properly
我有一个 UICollectionView,每个 UICollectionViewCell 都是自定义 class 和一个 UIImageView
@interface MyCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *image;
@end
然后我有一张图片我想放在图像视图中。但我无法让图像正确适合图像视图。这是图像。 those are the exact dimensions, I think that's a 36x36 but essentially I don't want the size to matter I just want it to scale down and fit regardless of the size. The dimensions of the UIImageView in the storyboard is 59x54 ( I know this can change depending on how the screen wants to display the cells so again size should not be relevant) These are the current display settings for the view in storyboard 还有我的 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;
}
结果看起来像这样 So you see how the edge is slightly cut off. I did read a few other answers of trying [cell.image setContentMode:UIViewContentModeScaleAspectFit];
but that didn't work either. Does anyone have a solution for this? The UIImageView is fit inside the UICollectionViewCell like so
外面的蓝线是UICollectionViewCell,里面的蓝线是UIImageView。
确保在图像视图和单元格之间添加顶部、前导、底部和尾随约束,以便在单元格更改大小时图像视图的大小正确 - 否则图像视图将保持在故事板,即使单元格大小发生变化。
您肯定还需要将图像视图的 Content Mode
(在故事板图像中看到)从 Scale to Fill
更改为 Aspect Fit
,以便图像在不拉伸的情况下缩放。
我有一个 UICollectionView,每个 UICollectionViewCell 都是自定义 class 和一个 UIImageView
@interface MyCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *image;
@end
然后我有一张图片我想放在图像视图中。但我无法让图像正确适合图像视图。这是图像。
- (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];
but that didn't work either. Does anyone have a solution for this? The UIImageView is fit inside the UICollectionViewCell like so
外面的蓝线是UICollectionViewCell,里面的蓝线是UIImageView。
确保在图像视图和单元格之间添加顶部、前导、底部和尾随约束,以便在单元格更改大小时图像视图的大小正确 - 否则图像视图将保持在故事板,即使单元格大小发生变化。
您肯定还需要将图像视图的 Content Mode
(在故事板图像中看到)从 Scale to Fill
更改为 Aspect Fit
,以便图像在不拉伸的情况下缩放。