簡體   English   中英

collectionView圖片:在Swift中動態更改高度

[英]collectionView Images: Change Height Dynamically in Swift

在這種情況下,圖像正在下載並顯示到collectionview ,並且工作正常,但是圖像以不同的方式顯示。 我已經顯示了所有圖像,它們都具有相同的高度和寬度。 如果圖像較小,則它將采用默認的圖像高度和寬度。 我已經顯示所有圖像都正確顯示了相同的高度和寬度。

在此處輸入圖片說明

   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

   let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "storeCell", for: indexPath) as? EShoppingStoreCollectionViewCell
        cell?.layer.borderColor = UIColor.groupTableViewBackground.cgColor
        cell?.layer.borderWidth = 0.5

        let urlData = storeDictionary?[indexPath.item]["logo"] as? String
        cell?.storeImageView.contentMode = UIViewContentMode.scaleAspectFit
        cell?.storeImageView.clipsToBounds = true
        let frame = CGRect(x: (cell?.storeImageView.frame.width)!/2 - 30, y: (cell?.storeImageView.frame.height)! - 30, width: 60, height: 60)

        if urlData != nil {
            let url = URL(string: urlData!)
            cell?.storeImageView.sd_setImage(with: url!, placeholderImage: UIImage(named:""), options: [SDWebImageOptions.continueInBackground, SDWebImageOptions.lowPriority, SDWebImageOptions.refreshCached, SDWebImageOptions.handleCookies, SDWebImageOptions.retryFailed]) { (image, error, cacheType, url) in
                if error != nil {
                    print("Failed: \(String(describing: error))")
                } else {
                    print("Success")
                }
            }
        }
    return cell!
}

如何將所有圖像設置為相同大小

image尺寸不在您的手中。 它是從API接收到的。 因此,它是更好地使用權分辨率的圖像與contentMode設置為scaleAspectFit而不是拉伸它。

不過,如果您希望在顯示器中使用相同的尺寸,請嘗試將scaleAspectFill contentMode更改為scaleAspectFillscaleToFill

cell?.storeImageView.contentMode = UIViewContentMode.scaleAspectFill

這肯定會拉伸圖像,但是您在顯示中看到的所有圖像都將覆蓋imageView的大小。

您還可以選擇另一種方式,會讓你的用戶界面更好看-設置backgroundColorimageView並將其設置為contentModescaleAspectFit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM