[英]UIImageView inside UICollectionViewCell image won't fit properly
我有一個 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。
確保在圖像視圖和單元格之間添加頂部、前導、底部和尾部約束,以便在單元格更改大小時正確調整圖像視圖的大小 - 否則即使單元格,圖像視圖也將保持在故事板中看到的大小改變大小。
您還肯定需要將圖像視圖的Content Mode
(在故事板圖像中看到)從“ Scale to Fill
到“ Aspect Fit
,以便圖像在不拉伸的情況下進行縮放。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.