2017-12-25 8 views
0

コレクションビューはUIViewによってラップされています。コレクションビューにはラッパービューと同じ境界があります。コレクションビューのデータソースが変更されるため、ラッパービューの境界も変更されます。私はラップトップビューintrinsicContentSizeのメソッドをcollectionView.collectionViewLayout.collectionViewContentSizeで実装しています。コレクションビューのレイアウトはサブクラスUICollectionViewFlowLayoutです。UICollectionViewのセルもメソッドintrinsicContentSizeを実装していますが、ラッパービューはコレクションビューのコンテンツサイズでフレームを更新しませんでした。 collectionViewContentSizeは常にCGRectZeroです。 私はUICollectionViewラッパービューフレームの更新

[self.layout invalidateLayout]; 
    [self.collectionView reloadData]; 
    [self.collectionView layoutIfNeeded]; 

を試みたが、うまくいきませんでした 私はcollectionView.collectionViewLayout.collectionViewContentSizeとラッパビューのフレームを更新するために何をすべき。

以下のいくつかのコードがあります:

ZXCollectionViewWrapperView *wrapperView = [[ZXCollectionViewWrapperView alloc] init]; 
    [self.view addSubview:wrapperView]; 
    [wrapperView mas_makeConstraints:^(MASConstraintMaker *make) { 
    make.center.equalTo(self.view); 
    }]; 
    self.wrapperView = wrapperView; 

    /// wrapper view size 
- (CGSize)intrinsicContentSize { 
    return self.collectionView.collectionViewLayout.collectionViewContentSize; 
} 


    ZXCollectionViewAlignedLayout *layout = [[ZXCollectionViewAlignedLayout alloc] init]; 
    self.layout = layout; 
    ZXCollectionView *collectionView = [[ZXCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; 
    collectionView.dataSource = self; 
    collectionView.delegate = self; 
    collectionView.backgroundColor = [UIColor lightGrayColor]; 
    [self addSubview:collectionView]; 
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) { 
    make.edges.equalTo(self); 
    }]; 
    self.collectionView = collectionView; 

    [self.collectionView zx_registerCellClass:[ZXCommendCell class]]; 

    self.dataSource = dataSource; 
    [self.collectionView reloadData]; 
    [self.collectionView layoutIfNeeded]; 

答えて

0

あなたが自動レイアウトを使用しているとして、あなたはラッパービューの高さ制約を作成する必要があります。

heightConstraint = make.height.equalTo(200) // initial height 

、あなたはあなたができる

heightConstraint.constant = collectionView.contentSize.height 

を変更して設定するにはコレクションビューのコンテンツサイズを観察するためにKVOを使用することができます。そして、あなたはこのように、同じコレクションビューのコンテンツサイズにその制約の定数を変更することができますcontentSizeを観察するためにこれを参照してください:observing contentSize (CGSize) with KVO in swift

+0

ラッパービューのUILabelのように、UICollectionView contentSizeによってラッパービューのサイズを調整したい(ラッパービューのUIlLabelがうまくいきました。 – jacinzhang

+0

あなたはスクロールビューであるのでコレクションビューでそれを行うことはできません – dduyduong

関連する問題