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

好吧,所以我在Swift 3工作,我是uicollectionviews的新手。 我试图以编程UICollectionReusableView在我的集合视图中添加标签作为标头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为中心。 我将文本对齐集中在一起,但文本偏向左侧:

在此处输入图像描述

如果它有帮助,我的集合视图在消息应用程序扩展中,但我再次不知道这会改变什么。 这有什么不对?

即使:

 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 

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

在此处输入图像描述

为什么?

您可以使用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) }