【问题标题】:CollectionViewLayout Height集合视图布局高度
【发布时间】:2018-04-20 05:03:53
【问题描述】:

我想通过使用UICollectionViewDelegateFlowLayout 为我的collectionView 的每一行自定义很多布局大小,如下所示:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = view.frame.width
        if indexPath.row == 0 {
            return CGSize(width: width, height: 300)
        }else if indexPath.row == 1 {
            return dynamicSize // i want this size wrap its content
        }
        return CGSize(width: width, height:100)
}

我想要动态大小,它可以为某行包装其内容。 有人知道怎么做吗?

这是我想要动态大小来包装它的单元格图像。

【问题讨论】:

  • 是的,我们知道,但首先向我们展示您的最终用户界面。
  • 我有更新问题,下面添加图片,请检查。

标签: swift uicollectionview uicollectionviewcell uicollectionviewlayout uicollectionviewdelegateflowlayout


【解决方案1】:

我建议你使用 tableview 而不是 collectionview。这对你来说很容易。

对于cellForHeight,返回UITableViewAutomaticDimension。它将自行计算您的单元格高度并进行相应调整。

注意:确保您的约束是正确的。

编辑:通过使用带有两列的collectionview。

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    var text = "Do any additional setup after loading the view, typically from a nib."

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{
        return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{

        let cellWidth = self.view.frame.size.width/2 - 10
        let textHeight = text.height(withConstrainedWidth: cellWidth, font: UIFont.systemFont(ofSize: 16))
        let constantHeight : CGFloat = 40 + 25 + 10 // top name view + bottom like button + margins height
        let totHeight = constantHeight + textHeight

        return CGSize(width: cellWidth, height: totHeight)
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCollectionViewCell", for: indexPath) as! MyCollectionViewCell

        cell.label.text = text
        cell.layer.borderWidth = 1.0
        cell.layer.borderColor = UIColor.black.cgColor
        return cell
    }

}

extension String {
    func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)

        return ceil(boundingBox.height)
    }

    func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)

        return ceil(boundingBox.width)
    }
}

输出:

【讨论】:

  • 在我在 tableView 中执行之前,它对我来说很好。但它有更多特殊的单元格,我需要将其更改为使用 collectionView。你还有什么好主意吗?
  • 你能具体说明为什么需要使用collectionview吗?
  • 我需要有 2 列的行。
  • @Brorsoeu.Sen 确保您的约束是正确的,否则您将面临一些新问题。请记住。
【解决方案2】:

使用此委托方法。这对我有用。

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
       return CGSize(width: Your cellWidth, height: YourcellHeight);
    }

【讨论】:

  • "YourcellHeight" 看起来像一个静态值,我需要动态值来包装我的内容。
  • 基本上您的单元格高度和宽度取决于您的设计。所以我只为你提供了这个委托。
  • 你的答案就是我在我的问题中写的!你看到了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-08
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多