带ContentInset的UICollectionView不是一直向下滚动

Ben*_*ten 16 scroll objective-c ios autolayout uicollectionview

我正在设置UICollectionView的内容插图:

[_collectionView setContentInset:UIEdgeInsetsMake(0.f, 0.f, 100.f, 0.f)];
Run Code Online (Sandbox Code Playgroud)

然后我用这种方法以编程方式一直滚动到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];
}
Run Code Online (Sandbox Code Playgroud)

它向下滚动,但它忽略了contentInset,这意味着最后一个单元格低于指定的内容插入:

在此输入图像描述 左侧图像显示了视图出现后现在的显示方式.在右图中,我手动向下滚动到最后一条消息.

我正在使用AutoLayout,任何想法为什么会发生这种情况?

编辑:

以下是IB设置的屏幕截图: 在此输入图像描述

Ben*_*ten 34

今天,我偶然发现了解决方案!

选择视图控制器并取消选中"调整滚动视图插图"选项.

在此输入图像描述

如果未选中此选项,iOS不会自动调整视图的插入内容(可能还有其子视图),这会导致我的问题...取消选中它并以编程方式配置滚动插件:

- (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)];
}
Run Code Online (Sandbox Code Playgroud)


Arj*_*una 9

如果您正在使用流布局,请尝试设置_collectionView.collectionViewLayout.sectionInset.


小智 6

快速版本

collectionview.contentInset = UIEdgeInsetsMake(44,0,0,0)
collectionview.scrollIndicatorInsets = UIEdgeInsetsMake(44,0,0,0)
Run Code Online (Sandbox Code Playgroud)