Swift 3:无法以编程方式在Collectionview头上正确创建标签?

时间:2016-10-12 01:50:37

标签: ios swift uicollectionview swift3 uicollectionviewcell

好吧,所以我在Swift 3工作,我是uicollectionviews的新手。我正在尝试以编程方式在我的集合视图中添加标签作为标头UICollectionReusableView的子视图。我像其他任何视图一样添加了标签,但我不能为我的生活中心标题上的标签。

这是自定义标题类中的标签代码,它添加到故事板中的标题:

let pointsLabel = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.customInit()
    }


    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.customInit()
    }

    func customInit() {

        //center inside content
        pointsLabel.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: 100)
    pointsLabel.center = CGPoint(x: self.bounds.width * 0.5, y: self.bounds.height * 0.5)
    pointsLabel.textColor = UIColor.darkGray
    pointsLabel.textAlignment = .center

        pointsLabel.text = "Testing 123"
        pointsLabel.font = UIFont(name: "Montserrat", size: 30)
        self.addSubview(pointsLabel)

    }

我的Collectionview确实有任何一方的保证金:

self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 32.5, bottom: 0, right: 32.5)

但是,这只会影响标题视图的大小,这意味着标签应该仍然以header.bounds.width * 0.5为中心。我将文本对齐集中在一起,但文本偏向左侧:

enter image description here

如果有帮助,我的收藏视图会在消息应用扩展程序中显示,但我再也不知道这会改变什么。这有什么不对?

即使:

pointsLabel.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: 100)
    //pointsLabel.center = CGPoint(x: self.bounds.width * 0.8, y: self.bounds.height * 0.5)
        pointsLabel.center = self.center
        pointsLabel.textColor = UIColor.darkGray
        pointsLabel.backgroundColor = UIColor.blue

或者将我的宽度改为更小,我得到:

enter image description here

为什么?

2 个答案:

答案 0 :(得分:0)

您可以使用Swift锚定系统让标签填充标题。您可以像这样创建自己的自定义标题类。锚定的工作方式是在标签上设置约束,以扩展到视图的顶部,左侧,右侧和底部,无论大小如何。通过将常量[32.5和-32.5]添加到左侧和右侧锚点,它将添加您要查找的插入间距。希望这会有所帮助。

自定义标题类

class CustomHeader: UICollectionViewCell {

    let pointsLabel: UILabel = {
     let n = UILabel()
         n.textColor = UIColor.darkGray
         n.textAlignment = .center
         n.text = "Testing 123"
         n.font = UIFont(name: "Montserrat", size: 30)
     return n
   }()

   override init(frame: CGRect) {
    super.init(frame: frame)
     addSubview(pointsLabel)

     pointsLabel.translatesAutoresizingMaskIntoConstraints = false
     pointsLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: 32.5).isActive = true
     pointsLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -32.5).isActive = true
     pointsLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
     pointsLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
   }
}

使用此标题的ViewController 您可以注册并声明您希望标题如下所示的宽度和高度。因此,您使用自定义标题的每个课程都会将以下功能添加到您的文件中,它应该为您提供您正在寻找的内容。 =]

class ControllerUsesHeader: UICollectionViewController {
  let headerId = "headerId"

  override func viewDidLoad() {
   super.viewDidLoad()

   collectionView?.register(CustomHeader.self, forSupplementaryViewOfKind:
        UICollectionElementKindSectionHeader, withReuseIdentifier: headerId)
  }


  override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind:
    String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:
            headerId, for: indexPath) as! CustomHeader
        return header
      }

} 

extension ControllerUsesHeader: UICollectionViewDelegateFlowLayout {
  func collectionView(_ collectionView: UICollectionView, layout 
    collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection 
    section: Int) -> CGSize {
    return CGSize(width: view.frame.width, height: 100)
}

答案 1 :(得分:0)

如果同时具有页眉和页脚,则可以用以下代码替换viewForSupplementaryElementOfKind函数

 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

       switch kind {
        case UICollectionElementKindSectionHeader:

            let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:
            headerId, for: indexPath) as! CustomHeader
            return header

        default:
            return UIHeaderView()
        }
}