2017-12-25 5 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 

다음 변경 모음보기 콘텐츠 크기를 관찰 KVO를 사용할 수 있으며 설정

heightConstraint.constant = collectionView.contentSize.height 

당신이 할 수있는 contentSize를 관찰하기 위해 이것을 참조하십시오 : observing contentSize (CGSize) with KVO in swift

+0

래퍼보기가있는 UILabel처럼 UICollectionView contentSize에 의해 래퍼보기 조정 크기가 필요합니다. (래퍼보기가있는 UIlLabel이 작동했습니다. – jacinzhang

+0

컬렉션보기에서는 스크롤보기이므로 할 수 없습니다. – dduyduong