自动调整UICollectionView标头的大小 [英] Auto-sizing UICollectionView headers

查看:195
本文介绍了自动调整UICollectionView标头的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为待办事项列表类型的应用制作详细信息屏幕.这是详细信息屏幕当前的外观:

I'm trying to make a detail screen for a to-do list kind of app. Here's what the detail screen currently looks like:

这是一个UICollectionViewController,带有标题.标头包含2个UILabel对象和一个UITextView对象.这些对象的布局由垂直的UIStackView管理. UIView用于设置白色背景.

This is a UICollectionViewController, with a header. The header contains 2 UILabel objects, and a UITextView object. The layout of these objects is managed by a vertical UIStackView. A UIView is used to set a white background.

在运行时定义此UICollectionReusableView的高度时遇到一些困难.任何建议表示赞赏.

I'm having some difficulties in defining the height of this UICollectionReusableView at runtime. Any advice is appreciated.

推荐答案

这有点骇人,但似乎可行.

This is a bit of a hack, but seems to work.

    // showhere to keep a reference
    UICollectionReusableView * _cachedHeaderView;


    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView 
               viewForSupplementaryElementOfKind:(NSString *)kind 
                                     atIndexPath:(NSIndexPath *)indexPath{

        if(!_cachedHeaderView){
            // dequeue the cell from storyboard
            _cachedHeaderView = 
         [collectionView dequeueReusableCellWithReuseIdentifier:@"header_cell" 
                                                   forIndexPath:indexPath];

            // set captions/images on the header etc...

            // tell the collectionview to redraw this section
          [self.collectionView reloadSections:[NSIndexSet 
                            indexSetWithIndex:indexPath.section]];
        }

        return _cachedHeaderView;
    }


        - (CGSize)collectionView:(UICollectionView *)collectionView 
                          layout:(UICollectionViewLayout*)collectionViewLayout        
 referenceSizeForHeaderInSection:(NSInteger)section{

        // once there is a reference ot the view, use it to figure out the height
        if(_cachedHeaderView){
            CGSize size = 
[_cachedHeaderView systemLayoutSizeFittingSize:collectionView.bounds.size 
                 withHorizontalFittingPriority:UILayoutPriorityRequired 
                       verticalFittingPriority:UILayoutPriorityDefaultLow];
            return size;

        }

        // a placeholder value just to get the dequeueReusableCellWithReuseIdentifier to work 
        return CGSizeMake(collectionView.bounds.size.width, 100);
    }

这篇关于自动调整UICollectionView标头的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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