iOS UICollectionView reloadData后立即獲取contentSize.height高度


 

自定義view重寫layoutsubviews 方法

核心屬性 listView.collectionViewLayout.collectionViewContentSize.height
tableview 同樣適用

- (void)layoutSubviews
{
    [super layoutSubviews];
    ///列表高度
    CGFloat listHeight = 0;
    
    if (self.isMoreAlert) {//高度高一些
        self.listBgView.backgroundColor = UIColor.whiteColor;
        [self.listBgView mas_updateConstraints:^(MASConstraintMaker *make) {
            if (@available(iOS 11.0, *)) {
                make.top.equalTo(self.mas_safeAreaLayoutGuideTop).offset(48.5);
            } else {
                make.top.equalTo(self).offset(48.5);
            }
        }];
        
        if (self.listView.collectionViewLayout.collectionViewContentSize.height > 420) {
            self.listView.showsVerticalScrollIndicator = YES;
        }
        listHeight = MIN(self.listView.collectionViewLayout.collectionViewContentSize.height, 420) ;
    }else{
        self.listBgView.backgroundColor = colorFromRGB(0xF6F7FB);
        listHeight = MIN(self.listView.collectionViewLayout.collectionViewContentSize.height, 265) ;
        if (self.listView.collectionViewLayout.collectionViewContentSize.height > 265) {
            self.listView.showsVerticalScrollIndicator = YES;
        }
    }
    
    [self.listView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(listHeight);
    }];
    
    [self.grayView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.listView);
        make.bottom.equalTo(self);
    }];
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM