之前項目中的需求:collectionView的頂部是一個視圖焰轻,隨著collectionView一起滾動臭觉,點擊頭部視圖中的一個按鈕,headerView的高度變高辱志,再次點擊還原蝠筑。
效果圖自己想象
/*
根據(jù)蘋果官方文檔說明,scrollView上最好不要再次添加scrollView揩懒,scrollView嵌套會比較麻煩
*/
思路:
1.自定義reusbleView什乙,添加頭部視圖
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
GGAutoScrollCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath];
view.delegate = self;
return view;
}
2.通過代理方式設(shè)置headerView的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
if(self.isType == YES){ //設(shè)置一個isType 初始化時將其設(shè)置為NO,返回的高度就是300
return CGSizeMake(CGRectGetWidth(self.view.frame),500);
}
return CGSizeMake(CGRectGetWidth(self.view.frame),300);
}
3.在自定義頂部視圖中添加一個代理方法
@protocol reusableViewDelegate <NSObject>
- (void)changeWithType:(BOOL)type;
@end
通過按鈕的點擊與否已球,傳遞狀態(tài)
- (void)click:(UIButton *)sender{
sender.selected = !sender.selected;//
if([self.delegate respondsToSelector:@selector(changeWithType:)]){
[self.delegate changeWithType:sender.selected];
}
}
4.根據(jù)狀態(tài)刷新整個collcetionView
- (void)changeWithType:(BOOL)type{
self.isType = type;
//通過按鈕的狀態(tài)臣镣,刷新數(shù)據(jù)更新headerView的高度 可自定義
[self.collectionView reloadData];
}