水平滚动集合视图单元格不根据内容宽度调整大小

问题描述

我已经像这样初始化了集合视图:

 override init(frame: CGRect){
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.itemSize = UICollectionViewFlowLayoutAutomaticSize
        layout.estimatedItemSize = CGSize(width: 100,height: 30)
        
        countryCollectionView = UICollectionView(frame: CGRect.zero,collectionViewLayout: layout)
        
        viewTitleLabel = UILabel()
        super.init(frame: frame)
        setupTableView()
        sortedDummyData = dummyData.sorted(by: <)
    }

我将大小返回为 CGSize.zero

    func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeforItemAt indexPath: IndexPath) -> CGSize {
//        return CGSize(width: 70,height: 30)
        return CGSize.zero
    }

我的单元格是这样定义的:

override init(frame: CGRect) {
        countryNameLabel = UILabel()
        countryUserCountLabel = UILabel()
        super.init(frame: frame)

        contentView.addSubview(countryNameLabel)
        countryNameLabel.translatesAutoresizingMaskIntoConstraints = false
        countryNameLabel.font = UIFont.boldSystemFont(ofSize: 13)
        countryNameLabel.textColor = .white
        
        contentView.addSubview(countryUserCountLabel)
        countryUserCountLabel.translatesAutoresizingMaskIntoConstraints = false
        countryUserCountLabel.font = UIFont.systemFont(ofSize: 13)
        countryUserCountLabel.textColor = .white
        
        NSLayoutConstraint.activate([
            countryNameLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor,constant: 5),countryNameLabel.heightAnchor.constraint(equalToConstant: 30),countryNameLabel.topAnchor.constraint(equalTo: self.topAnchor,countryNameLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: -5),//            countryNameLabel.widthAnchor.constraint(greaterThanorEqualToConstant: 0),//            countryNameLabel.widthAnchor.constraint(equalToConstant: 20),countryUserCountLabel.leadingAnchor.constraint(equalTo: countryNameLabel.trailingAnchor,countryUserCountLabel.topAnchor.constraint(equalTo: self.topAnchor,countryUserCountLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor,countryUserCountLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor,countryUserCountLabel.heightAnchor.constraint(equalToConstant: 30),countryUserCountLabel.widthAnchor.constraint(equalToConstant: 40)
        ])
    }
    

这是我得到的输出

解决方法

此更新对我有用:

返回非零单元格大小:

   func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 70,height: 30)
//        return CGSize.zero
    }

更新单元格约束:

        NSLayoutConstraint.activate([
            countryNameLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor,constant: 5),countryNameLabel.heightAnchor.constraint(equalToConstant: 30),countryNameLabel.topAnchor.constraint(equalTo: self.topAnchor,countryNameLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: -5),//            countryNameLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 0),//            countryNameLabel.widthAnchor.constraint(equalToConstant: 20),countryUserCountLabel.leadingAnchor.constraint(equalTo: countryNameLabel.trailingAnchor,countryUserCountLabel.topAnchor.constraint(equalTo: self.topAnchor,countryUserCountLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor,countryUserCountLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor,countryUserCountLabel.heightAnchor.constraint(equalToConstant: 30),// Removed the width constraint
//            countryUserCountLabel.widthAnchor.constraint(equalToConstant: 40)
        ])