【问题标题】:How to change the UICollectionView footerview's height programatically如何以编程方式更改 UICollectionView 页脚视图高度
【发布时间】:2015-01-09 16:49:01
【问题描述】:

我遇到了一个问题,在我的 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;
}

【问题讨论】:

    标签: uicollectionview uicollectionviewlayout


    【解决方案1】:

    顺便说一句,你的self.view.frame.size.height 是什么?我希望这绝对不会在任何时候为零。因为self.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);
        }
    }
    

    【讨论】:

      【解决方案2】:

      Swift 版本:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-14
        • 2013-08-08
        • 1970-01-01
        • 1970-01-01
        • 2021-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多