我正在尝试向我的 UICollectionViewController 添加 header 。我已经通过 Storyboard完成了这一切。当我运行该应用程序时,它没有显示。
这是我要在标题中显示的内容:
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", forIndexPath: indexPath) as! ProfileCollectionReusableView
header.editProfileButton.layer.cornerRadius = 5
header.nameLabel.adjustsFontSizeToFitWidth = true
header.nameLabel.text = PFUser.currentUser()!["name"] as? String
header.profilePictureImageView.layer.cornerRadius = header.profilePictureImageView.frame.size.height/2
header.profilePictureImageView.layer.masksToBounds = true
header.profilePictureImageView.layer.borderColor = UIColor.whiteColor().CGColor
return header
}
我在控制台上没有看到任何错误,应用程序也没有崩溃。只有一个白色屏幕,导航栏标题已更新为用户用户名。
最佳答案
如果使用 .xib
let nib1:UINib = UINib(nibName: "ScheduleHeaderView", bundle: nil)
self.myCollection.register(nib1, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "ScheduleHeaderView")
设置页眉大小
let layout = self.myCollection.collectionViewLayout as! UICollectionViewFlowLayout
layout.headerReferenceSize = CGSize(width: 50, height: 180)
UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header:ScheduleHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "ScheduleHeaderView", for: indexPath) as! ScheduleHeaderView
return header
}
关于ios - header 未显示在 UICollectionViewController Swift 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36612599/