I run into an issue that in my UICollectionView, I need to display a footer when the record returned is 0. The codes below seems working good, when there is no record, footer is displayed. However, there is an issue that when there are records, though footer is hidden, code LINE 1 seems do not have any effect, which means the blank space of footer is still there. Any idea how to get rid of it? i.e change the footer'e height to 0 , or just remove the footer when there is record returned.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
CGSize footerSize = CGSizeMake(320, self.view.frame.size.height);
return footerSize;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionFooter) {
reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
reusableview.hidden = YES;
if([topicArray count]>0){
reusableview.hidden = YES;
//reusableview.frame = CGRectMake(0, 0, 0, 0);
CGRect newFrame = reusableview.frame;
newFrame.size.height =0;
reusableview.frame = newFrame; // <-------- LINE 1.
}else{
reusableview.hidden = NO;
reusableview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
UIImageView *oopsImgView =[[UIImageView alloc] initWithFrame:CGRectMake(60, 130,200,154)];
UILabel *label=[[UILabel alloc] ....;
label.adjustsFontSizeToFitWidth = YES;
label.textAlignment = NSTextAlignmentCenter;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0;
label.font = [UIFont boldSystemFontOfSize:16.0f];
label.minimumScaleFactor = 10.0f/15.0f;
[reusableview addSubview:label];
}
}
return reusableview;
}
The approach for making this working is pretty simple, which involves adding a height constraint with the constant value set to collection view content height. Content height can be retrieved from the layout object. The constraint's constant value can be updated in viewWillLayoutSubviews.
Select the Collection Reusable View of the header, set the identifier as “HeaderView” in the Attributes inspector. For the Collection Reusable View of the footer, set the identifier as “FooterView”.
A view that defines the behavior for all cells and supplementary views presented by a collection view.
Btw, what is your self.view.frame.size.height
? I hope that's definitely not going to be zero in any moment. Because self.view
is the top view in the hierarchy inside the view controller.
You need not to change the footer size in viewForSupplementaryElementOfKind:
method. All you have to find is when you shouldn't show your footer in a particular section.
Try this and let me know if this works for you.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
BOOL sectionToHide = [self checkIfThisSectionToHide:section]; // find if the section to hide here..
if (sectionToHide) {
return CGSizeZero;
}else {
return CGSizeMake(320, self.view.frame.size.height);
}
}
Swift Version:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
if chackFlag // your condition
{
return CGSize(width: collectionView.width, height: 50)
}
else
{
return.zero
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With