自调整collectionview自动设置contentoffset零

Posted

技术标签:

【中文标题】自调整collectionview自动设置contentoffset零【英文标题】:self-sizing collectionview auto set contentoffset zero 【发布时间】:2017-06-02 07:46:15 【问题描述】:

我使用 UICollectionView flowlayout self-sizing 显示两个 textview 用于输入。 当用户输入时,我使布局无效

UICollectionViewLayoutInvalidationContext *context =
[[UICollectionViewFlowLayoutInvalidationContext alloc] init];
[context invalidateItemsAtIndexPaths:@[indexPath]];
context.contentSizeAdjustment = CGSizeMake(0, newHeight - oldHeight);
[self.flowlayout invalidateLayoutWithContext:context]; 

然后collectionview调用layoutSubViews,多次设置contentSize,因为contentSize太小,所以调用setContentOffsetIfNeed,然后offset为零。 我需要偏移保持原始值。请参阅下面的调用堆栈:

有人知道原因并解决了吗?

【问题讨论】:

【参考方案1】:

也许你可以使用[self.context sizeToFit] 来自行调整大小

【讨论】:

我没有发现 UICollectionViewLayoutInvalidationContext 有 sizeToFit 方法也没有 UICollectionViewLayoutAttributes

以上是关于自调整collectionview自动设置contentoffset零的主要内容,如果未能解决你的问题,请参考以下文章