如何在Swift中像Pinterest一样在UICollectionView中设置UIImage的动态高度

时间:2018-11-20 16:03:29

标签: ios swift uicollectionview uicollectionviewflowlayout

我正在尝试像Pinterest一样用于其图像库的布局,但是问题是我要返回图像高度以调整单元格的大小,并且当图像的高度不同时,它将在单元格中留出空间。我已经浏览了很多文章,但是没有找到一个简单的解决方案。有没有简单的方法可以做到这一点?感谢您的时间和帮助!

Please see this image

class ViewController: UIViewController {

    @IBOutlet var collectionVIew: UICollectionView!
    var imgData = [#imageLiteral(resourceName: "p1"),#imageLiteral(resourceName: "p2"),#imageLiteral(resourceName: "p3"),#imageLiteral(resourceName: "p4"),#imageLiteral(resourceName: "p5"),#imageLiteral(resourceName: "p6"),#imageLiteral(resourceName: "p7"),#imageLiteral(resourceName: "p8"),#imageLiteral(resourceName: "p1"),#imageLiteral(resourceName: "p2"),#imageLiteral(resourceName: "p3"),#imageLiteral(resourceName: "p4"),#imageLiteral(resourceName: "p5"),#imageLiteral(resourceName: "p6"),#imageLiteral(resourceName: "p7"),#imageLiteral(resourceName: "p8"),#imageLiteral(resourceName: "p1"),#imageLiteral(resourceName: "p2"),#imageLiteral(resourceName: "p3"),#imageLiteral(resourceName: "p4"),#imageLiteral(resourceName: "p5"),#imageLiteral(resourceName: "p6"),#imageLiteral(resourceName: "p7"),#imageLiteral(resourceName: "p8"),#imageLiteral(resourceName: "p1"),#imageLiteral(resourceName: "p2"),#imageLiteral(resourceName: "p3"),#imageLiteral(resourceName: "p4"),#imageLiteral(resourceName: "p5"),#imageLiteral(resourceName: "p6"),#imageLiteral(resourceName: "p7"),#imageLiteral(resourceName: "p8")]

    override func viewDidLoad() {
        super.viewDidLoad()

    }


}

extension ViewController: UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
                let width = collectionView.frame.size.width
    let img = imgData[indexPath.item]
   // 335 is the width of my cell image
    return CGSize(width: width/2-15, height: (img.size.height / 335) * width)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
}

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgData.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
        cell.imgView.image = imgData[indexPath.row];
        cell.imgView.contentMode = .scaleAspectFit
        return cell
    }
}

2 个答案:

答案 0 :(得分:0)

您正在尝试将非正方形图像放入一个正方形孔中。当我们这样做时,必须付出一些努力。在这种情况下,我建议您将每个单元格设置为固定高度,并在其上设置纵横比填充内容模式,以使它们完全填充单元格的框架。不利的一面是,一些较大的图像将被部分切除。另一种选择是使用宽高比适合内容模式,该模式将缩放图像以适合图像而不会丢失图像的一部分。这将使您再次具有单元格间距。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = collectionView.frame.size.width
    return CGSize(width: width/2-15, height: 100) // fixed height
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
    cell.imgView.image = imgData[indexPath.row];
    cell.imgView.contentMode = .scaleAspectFill // Set the scale mode to aspect fill
    return cell
}

或者,您可以签出一些可能为您实现此功能的开源库。这个看上去很有前途的https://github.com/abdullahselek/ASCollectionView

您可以在https://github.com/vsouza/awesome-ios#collection-view

处查看更大的馆藏视图开源库列表。

如果要使像元具有不同的高度,则需要根据图像的纵横比和固定的宽度计算正确的高度。在这种情况下,当我们的宽度固定并且高度动态时,您可以通过高度/宽度来计算宽高比。然后,将其乘以图像单元格的宽度即可得出动态单元格高度。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = collectionView.frame.size.width / 2 - 15;
    let img = imgData[indexPath.item]
    return CGSize(width: width, height: (img.size.height / img.size.width) * width)
}

在这种情况下,您应该将内容模式设置为适合方面。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
    cell.imgView.image = imgData[indexPath.row];
    cell.imgView.contentMode = .scaleAspectFit // Set the scale mode to aspect fit
    return cell
}

答案 1 :(得分:-1)

您提到的内容可以使用流布局实现-UIKit提供的布局类。

这是您要寻找的:

https://www.raywenderlich.com/392-uicollectionview-custom-layout-tutorial-pinterest

我希望您可以花些时间阅读这篇非常简单的文章,并仔细按照每个步骤进行操作。