与ContentInset的UICollectionView不滚动一路下去
我正在设置UICollectionView的内容:
[_collectionView setContentInset:UIEdgeInsetsMake(0.f, 0.f, 100.f, 0.f)];
然后我用这个方法以编程方式一直滚动到UICollectionView的底部:
- (void)scrollToLastMessageAnimated:(BOOL)animated; { if (_messages.count == 0) { return; } NSUInteger indexOfLastSection = _messagesBySections.count - 1; NSInteger indexOfMessageInLastSection = [_messagesBySections[indexOfLastSection] count] - 1; NSIndexPath *path = [NSIndexPath indexPathForItem:indexOfMessageInLastSection inSection:indexOfLastSection]; [_collectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:animated]; }
它向下滚动,但忽略了contentInset,这意味着最后的单元格在指定的内容插入点之下:
左边的图像显示了在视图出现之后它现在是如何显示的。 在正确的图像中,我手动滚动到最后一条消息。
我正在使用AutoLayout,为什么会发生这种情况?
编辑:
以下是IB设置的屏幕截图:
今天,偶然,我发现了解决scheme!
select你的视图控制器,并取消选中“调整滚动视图插入”选项。
有了这个选项未选中,iOS不会自动调整您的视图(也可能是它的子视图)的插入,导致我的问题…取消选中它,并以编程方式configuration您的滚动插入:
- (void)configureInsetsOfCollectionView { [_collectionView setContentInset: UIEdgeInsetsMake(self.navigationController.navigationBar.bounds.size.height + [UIApplication sharedApplication].statusBarFrame.size.height + DEFAULT_SPACING, 0.f, _keyboardHeight + _toolbar.bounds.size.height + DEFAULT_SPACING, 0.f)]; [_collectionView setScrollIndicatorInsets:UIEdgeInsetsMake(self.navigationController.navigationBar.bounds.size.height + [UIApplication sharedApplication].statusBarFrame.size.height, 0.f, _keyboardHeight + _toolbar.bounds.size.height, 0.f)]; }
如果您正在使用stream布局,请尝试设置_collectionView.collectionViewLayout.sectionInset。
可能的问题
您已经在工具栏下面设置了collectionView,并为两个视图的superview底部添加了约束。
解
将工具栏的约束条件设置为底部,将宽度和高度设置为固定大小。 为你的collectionView设置约束顶部,底部与工具栏,导致超视图和(替代)具有固定大小的宽度
更新
的CollectionView
按照以下步骤使其工作:
检查你的集合视图,不要把它放在工具栏下面,通过在文档大纲上select你的collectionView来添加这些约束,点击ctrl并拖动到你的视图,会出现一个popup窗口,按住shift并select这些约束。
工具栏
检查前面和底部,通过在视图中按Ctrl拖动。 并为工具栏添加固定的宽度和高度。
处理viewAppears之前的滚动
-(void)viewWillAppear:(BOOL)animated { [collectionView reloadData]; [self scrollToLastMessageAnimated:YES]; }
你可以在viewController类中设置referenceSizeForFooterInSection
函数:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { return CGSize(width: view.frame.width, height: 50) }
只需将高度设置为您所需的值。
迅捷的版本
collectionview.contentInset = UIEdgeInsetsMake(44,0,0,0) collectionview.scrollIndicatorInsets = UIEdgeInsetsMake(44,0,0,0)