31

セクションヘッダーの高さをUICollectionView動的に設定しようとしていますが、以下のコードを使用しても、何も変化していません。ビューは正しいアイテムで描画されますが、高さは変動しません。UICollectionViewこれが繰り返しの質問である場合は申し訳ありませんが、オブジェクトに特に関連するものは何も見つからないようです。前もって感謝します。

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{
    PhotoVideoHeaderCell *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                          withReuseIdentifier:@"videoHeaderView"
                                                                                 forIndexPath:indexPath];
    if (indexPath.section == 0) {
        // photos
        [headerView setSection:@"Photo"];
    } else {
        [headerView.VehicleDetailView removeFromSuperview];
        CGRect frame = headerView.frame;
        frame.size.height = 60;
        [headerView setFrame:frame];
        [headerView setNeedsDisplay];
        [headerView setBackgroundColor:[UIColor grayColor]];
        [headerView setSection:@"Video"];
    }

    return headerView;
}
4

4 に答える 4

77

フローレイアウトを使用していると仮定すると、デリゲートは次の関数を実装する必要があります。

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

ヘッダーごとに異なるサイズを返すことができます。水平方向にスクロールするコレクションビューでは、幅のみが使用されます。縦スクロールのものでは、高さのみが使用されます。未使用の値は無視されます—ビューは常に水平/垂直コレクションビューの高さ/幅全体を埋めるように引き伸ばされます。

フッターにも対応する方法があります。

于 2013-01-17T17:27:29.723 に答える
6

Swift 3および4で受け入れられた回答:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: collectionView.frame.size.width, height: 250)
}
于 2017-11-22T20:28:11.680 に答える
4

まあ、騒々しいデリゲートメソッドを使用する代わりに、私はこれを使用することを好みます:(一意のヘッダーサイズがある場合にのみ機能します)

    let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout // Assuming you use the default flow layout 
    layout.headerReferenceSize = CGSize(width: 42, height: 42) //Set the header size

itemSizeでも機能します。

    layout.itemSize = CGSize(width: 42, height: 42) //Set a custom item size

これは、たとえば画面の幅を基準にしてアイテムのサイズを設定する場合に最適です。

于 2018-04-05T07:51:52.323 に答える
1

次のようなものを試してください。

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *header = [siteMapCollection dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @"headerID" forIndexPath: indexPath];
    NSString *headerText = @"This is my header";
    UIFont *labFont = [UIFont fontWithName: @"HelveticaNeue-CondensedBold" size: 20.0];
    CGSize textSize = [dummyText sizeWithFont: labFont];
    UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, header.frame.size.height - (textSize.height + 12), header.frame.size.width, textSize.height + 8)];
    [headerLabel setFont: labFont];

    [header addSubview: headerLabel];

    return header;
}
于 2013-01-17T17:16:43.157 に答える