tableview中嵌套collectionview的使用场景经常见,一般都是collectionview高度写死,那么如何在tableview高度自适应的情况下,collectionview的高度还能动态算准,可以通过以下方式,
在cell中重写-(CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority方法
-(CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority {
CGSize size = [super systemLayoutSizeFittingSize:targetSize withHorizontalFittingPriority:horizontalFittingPriority verticalFittingPriority:verticalFittingPriority];
[self.collectionView layoutIfNeeded];
CGFloat height = self.collectionView.collectionViewLayout.collectionViewContentSize.height;;
return CGSizeMake(size.width, height);
}
如果是tableview中嵌套tableview,需要动态计算高度的,可在cell中重写该方法:
-(CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority{
CGSize size = [super systemLayoutSizeFittingSize:targetSize withHorizontalFittingPriority:horizontalFittingPriority verticalFittingPriority:verticalFittingPriority];
[self.tableView layoutIfNeeded];
CGFloat heigth = self.tableView.contentSize.height;
return CGSizeMake(size.width, heigth);
}
标签:iOS,tableView,withHorizontalFittingPriority,collectionView,UILayoutPriority,vert
From: https://www.cnblogs.com/qqcc1388/p/17560519.html