如何解决为什么它不直接显示正确大小的内容?
我想单击按钮,然后显示警报视图。此警报视图将接收两个数组。
警报视图将以两个UICollectionView布局大小刷新自身。
[self.topCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:self.topSelectIndex inSection:0] animated:false scrollPosition:UICollectionViewScrollPositionBottom];
[self.bottomCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:self.bottomSelectIndex inSection:0] animated:false scrollPosition:UICollectionViewScrollPositionBottom];
[self layoutIfNeeded];
CGFloat topFloatHeight = self.topCollectionView.collectionViewLayout.collectionViewContentSize.height;
CGFloat bottomFloatHeight = self.bottomCollectionView.collectionViewLayout.collectionViewContentSize.height;
self.topCollectionHeightConstrait.constant = topFloatHeight;
self.bottomCollectionHeightConstraint.constant = bottomFloatHeight;
// CGFloat bigScrollViewHeight = self.bigScrollView.contentLayoutGuide.heightAnchor
CGFloat catHeight = topFloatHeight + bottomFloatHeight + 200;
CGFloat screenHeight = kScreenHeight - 180;
if( catHeight < screenHeight) {
self.scrollBoxHeightCont.constant = catHeight;
}else {
self.scrollBoxHeightCont.constant = screenHeight;
}
self.bigScrollView.contentSize = CGSizeMake(0,catHeight);
[self.topCollectionView reloadData];
[self.bottomCollectionView reloadData];
如您所见,关键代码是
self.bigScrollView.contentSize = CGSizeMake(0,catHeight);
代码更改了bigScrollview的contentSize,警报视图始终显示下一次而不是这次在上述代码内容中计算出的正确大小。
为什么它不直接显示正确大小的内容? 获得相同结果的其他解决方案在哪里?
解决方法
收集后,视图将重新加载新数据。 在显示客户视图之前,添加两个行代码:
[self setNeedsLayout];
[self layoutIfNeeded];
将计算高度代码移至layoutSubviews
方法中。
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat topFloatHeight = self.topCollectionView.collectionViewLayout.collectionViewContentSize.height;
CGFloat bottomFloatHeight = self.bottomCollectionView.collectionViewLayout.collectionViewContentSize.height;
self.topCollectionHeightConstrait.constant = topFloatHeight;
self.bottomCollectionHeightConstraint.constant = bottomFloatHeight;
// CGFloat bigScrollViewHeight = self.bigScrollView.contentLayoutGuide.heightAnchor
CGFloat catHeight = topFloatHeight + bottomFloatHeight + 200;
CGFloat screenHeight = kScreenHeight - 180;
if( catHeight < screenHeight) {
self.scrollBoxHeightCont.constant = catHeight;
}else {
self.scrollBoxHeightCont.constant = screenHeight;
}
self.bigScrollView.contentSize = CGSizeMake(0,catHeight);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。