How to change the UICollectionView footerview'

2019-01-25 23:02发布

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;
}

1条回答
戒情不戒烟
2楼-- · 2019-01-25 23:39

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);
    }
}
查看更多
登录 后发表回答