ios - header 未显示在 UICollectionViewController Swift 中

标签 ios xcode swift uicollectionview

我正在尝试向我的 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/

相关文章:

python - 苹果 API/SDK : requestUserToken without Swift/Objective-C?

ios - 如何从旋转木马中拖动 View

ios - 如何在开发环境中测试 Smart App Banner Urls

iphone - 如何检测 uitableViewCell 上触摸的按钮并更改该按钮的 BG 图像

objective-c - 使用最新的 xcode/OSX 版本构建项目时出现奇怪的应用程序行为

ios - 解析用户名/密码登录错误

iphone - 我怎样才能使它成为类的私有(private)成员或方法,以便可以在静态库本身而不是库外部访问它?

ios - 向核心数据中输入plist数据

iphone - 带有 Xcode 4.2 的 Interface Builder 中的 UIApplicationDelegate

ios - 了解 Parse Geopoint 和 CoreLocation 之间的区别