自定义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); }]; }