swift - CollectionViewLayout 高度

标签 swift uicollectionview uicollectionviewcell uicollectionviewlayout uicollectionviewdelegateflowlayout

我想通过使用 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)
}

我想要 dynamicSize 可以将其内容包装在某行中。 有人知道怎么做吗?

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

enter image description here

最佳答案

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

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

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

Edit: By using collectionview with two columns.

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)
    }
}

输出:

enter image description here

关于swift - CollectionViewLayout 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49934335/

相关文章:

ios - 尝试呈现其 View 不在窗口层次结构中的 UIAlertController - Swift 错误

ios - 在 iOS 设备中启动 YouTube 应用程序

ios - 异步函数不更新变量

ios - 可重用的 UICollectionViewCell 不刷新

ios - UICollectionview 在错误的地方更新随机图像

ios - 带有Tap Gesture Recognizer的UILabel无法正常工作

ios - UICollectionView 未以编程方式加载

swift - 无法将项目插入 NSOutlineView

ios - UICollectionViewCell 呼吸时圆角恢复为方形

ios - CollectionViewCell 不显示 TextView