xcode 标题未显示在 UICollectionViewController Swift 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36612599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Header not showing in UICollectionViewController Swift
提问by 01Riv
I am trying to add a header to my UICollectionViewController. I have done this all through storyboard. When I run the app it doesn't show up.
我正在尝试向我的 UICollectionViewController 添加标题。我已经通过故事板完成了这一切。当我运行该应用程序时,它不显示。
This is what I am trying to display in my header:
这就是我试图在我的标题中显示的内容:
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
}
I am not seeing any errors on the console nor does the app crash. There is just a white screen with the navigation bars title updated to the users username.
我在控制台上没有看到任何错误,应用程序也没有崩溃。只有一个白色屏幕,导航栏标题更新为用户用户名。
回答by Supapon Pick Pucknavin
if use .xib
如果使用 .xib
let nib1:UINib = UINib(nibName: "ScheduleHeaderView", bundle: nil)
self.myCollection.register(nib1, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "ScheduleHeaderView")
set size for header
设置标题大小
let layout = self.myCollection.collectionViewLayout as! UICollectionViewFlowLayout
layout.headerReferenceSize = CGSize(width: 50, height: 180)
UICollectionViewDataSource
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
}
回答by Benjamin
Here are some tips you need to pay attention to what you have to do:
以下是一些您需要注意的提示:
1.Implement UICollectionViewDelegateFlowLayout like:
1.实现 UICollectionViewDelegateFlowLayout 像:
class MyCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {}
2.register header class before you use :
2.使用前注册头类:
self.collectionView!.registerClass(HeaderClass.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
in the method ViewDidLoad();
在方法 ViewDidLoad() 中;
3.set the size in the UICollectionViewDelegateFlowLayout like UITableViewDelegate
3.在 UICollectionViewDelegateFlowLayout 中设置大小,如 UITableViewDelegate
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSizeMake(Config.PHONE_WIDTH, 55)
}
4.return your own header like what you already done:
4.像你已经完成的那样返回你自己的标题:
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
}
回答by shaangon
The issue could very well be with the height of the header section. I encountered the similar issue where I was registering the header but then the height was not set.
问题很可能与标题部分的高度有关。我在注册标题时遇到了类似的问题,但未设置高度。
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 20, height: 20)
layout.minimumInteritemSpacing = 10
layout.minimumLineSpacing = 10
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collectionView.backgroundColor = UIColor.white //default background color is black
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.register(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCell")
//setting the height helped it in displaying on the page
//设置高度有助于它在页面上显示
layout.headerReferenceSize = CGSize(width: 50, height: 180)
回答by ayoub
To show header just make sure you have this code in your code with number "1", like this:
要显示标题,请确保您的代码中有此代码,编号为“1”,如下所示:
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}