이 내가 헤더로 내 자신의 뷰를 추가 한 단계입니다은 어떻게 UICollectionView에 헤더보기를 추가하는
새보기 만들기 :
class HeaderView: UICollectionReusableView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.backgroundColor = .red
}
}
이 viewDidLoad
내부의 뷰를 등록이 :
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width:view.frame.size.width, height:100.0)
}
:
self.collectionView?.register(HeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
referenceSizeForHeaderInSection
대리자를 구현
는 viewForSupplementaryElementOfKind
위임 구현 :
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header", for: indexPath) as! HeaderView
header.setNeedsLayout()
return header
default:
return UICollectionReusableView()
}
}
을하지만 그것은 작동하지 않습니다. 내 referenceSizeForHeaderInSection
및 viewForSupplementaryElementOfKind
은 앱을 실행할 때 호출되지 않습니다.
나는 viewDidLoad
내부 설정 내 대표는 않았다
self.collectionView?.delegate = self
나는 어떤 단계를 실종? 아니면 내가 뭔가 잘못 했니?
편집 :
는 얘기를 깜빡 했네요 : 당신이Section Header
옵션이 사라 레이아웃 사용자 정의 선택할 때이 문제가 있는지 확실하지 않습니다, 사용자 정의 레이아웃을 사용하지만, 스토리 보드에서하고 있습니다.
컬렉션보기에 하나 이상의 섹션이 포함되어 있는지 확인하십시오. –
예. 그것은 단지 하나의 섹션을 가지고 있습니다. 나는 내 데이터를 볼 수 있지만 헤더는 볼 수 없다. – Kobe
'referenceSizeForHeaderInSection'은 레이아웃보기 대리자가 아닌 레이아웃 위임자의 메서드입니다. 레이아웃의 델리게이트도 설정 했습니까? –