Hi,
I create UILabel by Adding it as a subview to header...
Here is my code for Header at '0'...
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) - UICollectionReusableView {
let reusableview = UICollectionReusableView()
if (kind == UICollectionView.elementKindSectionHeader) {
print("section \(indexPath.section)")
if indexPath.section == 0 {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: collectionViewFirstHeaderFooterReuseIdentifier, for: indexPath)
headerView.backgroundColor = .clear
let headerUILabelText = UILabel()
headerUILabelText.textAlignment = .left
headerUILabelText.font = UIFont.systemFont(ofSize: 20, weight: .bold)
headerUILabelText.numberOfLines = 0
headerUILabelText.text = "Your Roll Number is \(NumberInteger). Your Exam Numbers are \(examNumbers(number: "\(NumberInteger)"))"
headerUILabelText.textColor = .darkGray
headerUILabelText.lineBreakMode = .byTruncatingTail
headerView.addSubview(headerUILabelText)
headerUILabelText.setMargins()
headerUILabelText.isHidden = false
headerUILabelText.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
headerUILabelText.leadingAnchor.constraint(equalTo: headerView.leadingAnchor),
headerUILabelText.trailingAnchor.constraint(equalTo: headerView.trailingAnchor),
headerUILabelText.centerXAnchor.constraint(equalTo: headerView.centerXAnchor),
headerUILabelText.centerYAnchor.constraint(equalTo: headerView.centerYAnchor),
headerUILabelText.topAnchor.constraint(equalTo: headerView.topAnchor),
headerUILabelText.rightAnchor.constraint(equalTo: headerView.rightAnchor),
headerUILabelText.bottomAnchor.constraint(equalTo: headerView.bottomAnchor),
headerUILabelText.leftAnchor.constraint(equalTo: headerView.leftAnchor),
headerUILabelText.widthAnchor.constraint(equalToConstant: headerView.frame.width),
headerUILabelText.heightAnchor.constraint(equalToConstant: headerView.frame.height)
])
return headerView
} else if indexPath.section == 1 {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: collectionViewSecondHeaderFooterReuseIdentifier, for: indexPath)
return headerView
} else {
return reusableview
}
}