如何以编程方式更改 UICollectionView 页脚视图的高度 [英] How to change the UICollectionView footerview's height programatically

查看:23
本文介绍了如何以编程方式更改 UICollectionView 页脚视图的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,在我的 UICollectionView 中,当返回的记录为 0 时,我需要显示一个页脚.下面的代码似乎运行良好,当没有记录时,显示页脚.但是,有一个问题,当有记录时,虽然footer被隐藏了,但是代码LINE 1似乎没有任何作用,也就是说footer的空格还在.知道如何摆脱它吗?即将页脚高度更改为 0 ,或者在返回记录时删除页脚.

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

推荐答案

顺便说一句,你的 self.view.frame.size.height 是多少?我希望这在任何时候都不会为零.因为 self.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.

您无需在viewForSupplementaryElementOfKind: 方法中更改页脚大小.您只需找到什么时候不应该在特定部分显示页脚.

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

这篇关于如何以编程方式更改 UICollectionView 页脚视图的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆