【问题标题】:Horizontal scrolling collection view cells not resizing based on content width水平滚动集合视图单元格不根据内容宽度调整大小
【发布时间】:2021-04-17 05:28:39
【问题描述】:

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

 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, constant: 5),
            countryNameLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -5),
//            countryNameLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 0),
//            countryNameLabel.widthAnchor.constraint(equalToConstant: 20),

            countryUserCountLabel.leadingAnchor.constraint(equalTo: countryNameLabel.trailingAnchor, constant: 5),
            countryUserCountLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 5),
            countryUserCountLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -5),
            countryUserCountLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -5),
            countryUserCountLabel.heightAnchor.constraint(equalToConstant: 30),
            countryUserCountLabel.widthAnchor.constraint(equalToConstant: 40)
        ])
    }
    

这是我得到的输出:

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout


    【解决方案1】:

    此更新对我有用:

    返回非零单元格大小:

       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, constant: 5),
                countryNameLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -5),
    //            countryNameLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 0),
    //            countryNameLabel.widthAnchor.constraint(equalToConstant: 20),
    
                countryUserCountLabel.leadingAnchor.constraint(equalTo: countryNameLabel.trailingAnchor, constant: 5),
                countryUserCountLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 5),
                countryUserCountLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -5),
                countryUserCountLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -5),
                countryUserCountLabel.heightAnchor.constraint(equalToConstant: 30),
    
    // Removed the width constraint
    //            countryUserCountLabel.widthAnchor.constraint(equalToConstant: 40)
            ])
    

    【讨论】:

      猜你喜欢
      • 2019-07-11
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 2015-10-03
      • 2021-09-28
      • 2016-09-30
      • 2017-02-28
      • 2016-02-02
      相关资源
      最近更新 更多