Alright, so Im working in Swift 3 here and Im new to uicollectionviews. I am trying to programmatically add a label as a subview of a header UICollectionReusableView
in my collection view. I have added the label like any other view, but I cant for the life of me CENTER the label on the header.
Here is my label code in the custom header class, which is added to the header in the storyboard:
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)
}
My Collectionview does have a margin on either side:
self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 32.5, bottom: 0, right: 32.5)
However that should just affect the size of the header view, meaning the label should still be centered at the header.bounds.width * 0.5. And I centered the text alignment, yet the text is skewed to the left:
If it helps, my collection view is within an message app extension, but again I don't know how that would change things. What is wrong here?
Even with:
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
Or changing my width to less, I get:
Why?