带有 ContentInset 的 UICollectionView 不会一直向下滚动

2023-09-10移动开发问题
26

本文介绍了带有 ContentInset 的 UICollectionView 不会一直向下滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I am setting the content inset of a UICollectionView:

[_collectionView setContentInset:UIEdgeInsetsMake(0.f, 0.f, 100.f, 0.f)];

然后我用这个方法以编程方式一直滚动到 UICollectionView 的底部:

Then I am scrolling programmatically all the way to the bottom of the UICollectionView with this method:

- (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,意味着最后一个单元格在指定的content inset之下:

It is scrolling down, but it is ignoring the contentInset, meaning that the last cells are below the specified content inset:

左图显示了视图出现后它现在的样子.在右图中,我手动向下滚动到最后一条消息.

The left image, shows how it appear now after the view did appear. In the right image, I manually scrolled further down to the last message.

我正在使用 AutoLayout,你知道为什么会这样吗?

I am using AutoLayout, any ideas why this happens?

这是 IB 设置的屏幕截图:

Here is a screenshot of the IB setup:

推荐答案

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

Today, by chance I discovered the solution!

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

Select your view controller and uncheck the option "Adjust Scroll View Insets".

如果未选中此选项,iOS 不会自动调整视图的插图(可能还有它的子视图),这给我带来了问题...取消选中它并像这样以编程方式配置滚动插图:

With this option unchecked, iOS does not automatically adjust your insets of the view (and probably its subviews), which caused the problems for me ... Uncheck it and configure your scroll insets like this programmatically:

- (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)];
}

这篇关于带有 ContentInset 的 UICollectionView 不会一直向下滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

如何将 32 位 PNG 转换为 RGB565?
How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)...
2024-08-12 移动开发问题
21

正确的 cocos2d 场景重启?
Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)...
2024-08-12 移动开发问题
7