UICollectionView 헤더가 표시되지 않음
-
12-12-2019 - |
문제
나는 UICollectionView
여러 앨범을 보여드리기 위해항목은 잘 표시되지만 이제 첫 번째 섹션 위에 헤더를 표시하고 싶습니다.
이를 위해 다음을 추가했습니다. registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
내 초기화 방법에.이와 같이:
[self.collectionView registerNib:[UINib nibWithNibName:@"AlbumHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kAlbumHeaderIdentifier];
(그만큼 AlbumHeader
Nib에는 클래스 뷰가 포함되어 있습니다. AlbumHeader
, 이는 의 하위 클래스입니다. UICollectionView
.)
그 이후에는 구현을 했는데요 collectionView:viewForSupplementaryElementOfKind:atIndexPath
방법:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
return [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kAlbumHeaderIdentifier forIndexPath:indexPath];
}
이제 헤더 뷰를 로드하려고 시도해야 할 것 같습니다.하지만 그렇지 않으면 보충 보기에 대한 메서드가 호출되지 않습니다.
내가 무엇을 놓치고 있나요?몇 시간 동안 멈췄습니다. 다음 문서를 읽어보세요. UICollectionView
여러 번 시도했지만 아무것도 도움이 되지 않는 것 같습니다.이견있는 사람?
해결책
yuf가 요청한 방법을 찾은 후 기본적으로 머리글/바닥글의 크기는 0,0이라는 것을 읽었습니다.크기가 0이면 머리글/바닥글이 표시되지 않습니다.
속성을 사용하여 크기를 설정할 수 있습니다.
flowLayout.headerReferenceSize = CGSizeMake(0, 100);
그러면 모든 헤더의 크기가 동일해집니다.섹션마다 달라야 하는 경우 다음 메서드를 구현할 수 있습니다. UICollectionViewDelegateFlowLayout
규약.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
if (section == albumSection) {
return CGSizeMake(0, 100);
}
return CGSizeZero;
}
세로 스크롤에서는 반환된 값을 사용합니다. height
컬렉션 뷰의 전체 너비, 가로 스크롤에서는 리턴을 사용합니다. width
컬렉션 뷰의 전체 높이.
다른 팁
다음을 구현했나요?
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
한 가지를 작동시키기 위해 구현해야 할 방법이 엄청나게 많습니다. 저도 배우고 있습니다.작동하는지 알려주세요.
편집하다:죄송합니다. 잘못된 방법입니다.내 생각에 그것은 서브클래싱을 위한 것입니다.내가 말하는 것은 UICollectionViewLayout
(레이아웃이 보충 뷰를 지원하는 경우 하위 클래스로 분류한 레이아웃 객체):
- layoutAttributesForSupplementaryViewOfKind:atIndexPath:
~을 위한 스위프트 3 & 스위프트 4
self.collectionView.register(UINib(nibName: "SectionCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionCollectionReusableView")
self.collectionView.fs_width = self.collectionView.bounds.width
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 70, left: 40, bottom: 0, right: 0)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.estimatedItemSize = CGSize(width: 350, height: 140)
layout.scrollDirection = .horizontal
layout.headerReferenceSize = CGSize(width: self.collectionView.bounds.size.width, height: 60)
layout.sectionHeadersPinToVisibleBounds = true
self.collectionView!.collectionViewLayout = layout
이것을 추가해야합니다 보기DidLoad, 그리고 다음을 확인하세요.
layout.headerReferenceSize = CGSize(width: self.collectionView.bounds.size.width, height: 60)
이렇게 하면 헤더 섹션 레이아웃이 만들어집니다.
그리고 @Guido Hendriks에게 감사드립니다. 그리고 제가 얻은 모든 것은 그들의 답변에서 얻은 통찰력이기도 합니다.