uicollectionview - 如何以编程方式更改 UICollectionView 页脚 View 高度

标签 uicollectionview uicollectionviewlayout

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

- (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是 View Controller 内部层次结构中的顶 View 。

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

试试这个,让我知道这是否适合你。

- (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 - 如何以编程方式更改 UICollectionView 页脚 View 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27865301/

相关文章:

swift - collectionView.dequeueReusableCell 不调用 Cell 的 init

swift - 如何制作水平滚动的 2 行多列 UICollectionView

ios - swift 中的自定义 Collection View 单元格

ios - 在 ViewController 中访问 UICollectionView 单元格

ios - xcode如何在数据库中的 Collection View 单元格中显示图像

swift - 相对于框架宽度和纵横比的动态 UICollectionViewCell 宽度

ios - 在没有重新加载方法的情况下调整 UICollectionView 的单元格大小

ios - 以编程方式在 UICollectionView 中对齐 UILabel 大小

ios - UICollectionView 不显示单元格和崩溃应用程序 Xcode 10.0

ios - collectionViewContentSize() 与 contentSize