【问题标题】:Overlapping text in uicollection view headers for different sections不同部分的集合视图标题中的重叠文本
【发布时间】:2020-07-08 17:38:08
【问题描述】:

我在 swift 中为每个部分的 collectionview 标题添加了不同的文本标签。我使用 uireusableviews 作为标题。我的问题是当我滚动不同部分标签中的文本相互重叠时。我尝试在更改文本之前将文本标签设置为“”,但问题仍然存在。

 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if(kind == UICollectionView.elementKindSectionHeader){
       let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "h", for: indexPath) as? UICollectionViewCell
        let txtlab = UILabel()
        txtlab.text = ""
        if(indexPath.section%2 == 0){
            txtlab.text = "Header"}
        else{
            txtlab.text = "another header"
        }
        cell?.addSubview(txtlab)
        txtlab.translatesAutoresizingMaskIntoConstraints = false
        txtlab.centerYAnchor.constraint(equalTo: cell!.centerYAnchor).isActive = true
        txtlab.centerXAnchor.constraint(equalTo: cell!.centerXAnchor).isActive = true
        cell?.backgroundColor = .green
        return cell!
    }

Image of problem faced

【问题讨论】:

    标签: swift uicollectionview uicollectionviewlayout uicollectionreusableview


    【解决方案1】:

    您需要在生成新视图之前清除视图。

    将创建标题的方法放在顶部。

    for view in myView {
    myView.removeFromSuperview()
    }
    

    编辑,我没有看到你的代码。

    您的第一个问题是您要为每个单元格创建一个新标签。您应该创建一个插座,以便每个单元格都连接到标签。然后你应该可以更改标题。

    【讨论】:

      猜你喜欢
      • 2019-12-31
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多