【问题标题】:Swift: Estimated Height Not Working for UICollectionViewSwift:估计的高度不适用于 UICollectionView
【发布时间】:2021-04-06 14:41:39
【问题描述】:

我的问题是为我的集合视图布局设置估计高度不起作用。具体来说,单元格不会动态调整大小。

我的假设是我的单元格上的 AutoLayout 约束设置不正确/UILabel


首先,我创建了一个自定义UICollectionViewCell

import UIKit
import SnapKit


class BubbleCell : UICollectionViewCell {
    
    var label:UILabel!
    var spacer:UIView!
    
    var text:String = "" {
        didSet {
            guard label != nil else {return}
            label.text = text
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        label = UILabel()
        label.textColor = .white
        label.numberOfLines = 0
        contentView.addSubview(label)
        
        spacer = UIView()
        contentView.addSubview(spacer)
        
        label.snp.makeConstraints { (make) in
            make.width.lessThanOrEqualToSuperview().multipliedBy(0.7)
            make.top.right.equalTo(self.safeAreaLayoutGuide).inset(10)
        }
        
        spacer.snp.makeConstraints { (make) in
            make.top.equalTo(self.label.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
}

接下来,我创建了一个自定义UICollectionView。下面是我用来生成“布局”的函数

func getLayout() -> UICollectionViewCompositionalLayout {
    let size = NSCollectionLayoutSize.init(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(200))
    let item = NSCollectionLayoutItem.init(layoutSize: size)
    let group = NSCollectionLayoutGroup.horizontal(layoutSize: size, subitems: [item])
    let section = NSCollectionLayoutSection.init(group: group)
    return UICollectionViewCompositionalLayout.init(section: section)
}

现在,当我运行我的应用程序时,您可以看到单元格(红色背景)没有调整其高度以适应我的UILabel。我做错了什么?

【问题讨论】:

    标签: ios swift user-interface autolayout snapkit


    【解决方案1】:

    通过像这样实现我的类来解决。

    import UIKit
    import SnapKit
    
    
    class BubbleCell : UICollectionViewCell {
        
        var label:UILabel!
        var background:UIView!
        
        var text:String = "" {
            didSet {
                guard label != nil else {return}
                label.text = text
            }
        }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupUI()
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        func setupUI(spacing:CGFloat=2) {
                        
            label = UILabel()
            label.textColor = .white
            label.numberOfLines = 0
            contentView.addSubview(label)
            
            background = UIView()
            background.backgroundColor = .systemBlue
            background.layer.cornerRadius = 17
            background.clipsToBounds = true
            contentView.addSubview(background)
            contentView.sendSubviewToBack(background)
                    
            label.snp.makeConstraints { (make) in
                make.top.bottom.equalToSuperview().inset(10 + spacing)
                make.right.equalToSuperview().inset(20)
                make.width.lessThanOrEqualToSuperview().multipliedBy(0.7)
            }
            
            background.snp.makeConstraints { (make) in
                make.top.equalTo(self.label).inset(-10)
                make.bottom.equalTo(self.label).offset(10)
                make.left.equalTo(self.label).inset(-15)
                make.right.equalTo(self.label).offset(15)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      相关资源
      最近更新 更多