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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23993409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:15:23  来源:igfitidea点击:

UICollectionView with ContentInset is not Scrolling all the Way Down

iosobjective-cscrolluicollectionviewautolayout

提问by electronix384128

I am setting the content inset of a UICollectionView:

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

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

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

然后我用这个方法以编程方式滚动到 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];
}

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

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

enter image description hereThe 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.

在此处输入图片说明左图显示了视图出现后它现在的样子。在右图中,我手动向下滚动到最后一条消息。

I am using AutoLayout, any ideas why this happens?

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

EDIT:

编辑:

Here is a screenshot of the IB setup: enter image description here

这是IB设置的屏幕截图: 在此处输入图片说明

回答by electronix384128

Today, by chance I discovered the solution!

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

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

选择您的视图控制器并取消选中“调整滚动视图插入”选项。

enter image description here

在此处输入图片说明

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:

取消选中此选项后,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)];
}

回答by Arjuna

If you are using flow layout try to set _collectionView.collectionViewLayout.sectionInset.

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

回答by Yoni Katz

swift version

快速版

collectionview.contentInset = UIEdgeInsetsMake(44,0,0,0)
collectionview.scrollIndicatorInsets = UIEdgeInsetsMake(44,0,0,0)

回答by E-Riddie

Possible Issue

可能的问题

You have set the collectionView below the toolbar and added constraints to bottom of superview for both views.

您已在工具栏下方设置了 collectionView 并为两个视图的超级视图底部添加了约束。

Solution

解决方案

Set the constraints of the toolbar to bottom, leading and set the width and height to fixed size. For your collectionView set the constraints to top, to bottom with the toolbar, leading to the superview and (alternative) with fixed size of width

将工具栏的约束设置为底部、前导并将宽度和高度设置为固定大小。对于您的 collectionView,使用工具栏将约束设置为顶部,底部,导致超级视图和(替代)具有固定大小的宽度

Update

更新

CollectionView

集合视图

Follow these steps to make it work:

请按照以下步骤使其工作:

Check your collection view, and don't put it below toolbar, and add these constraints by selecting your collectionView on Document Outline, click ctrl and drag it to your view, a popup will appear, hold shift and select these constraints.

检查您的集合视图,不要将其放在工具栏下方,并通过在文档大纲上选择您的集合视图来添加这些约束,单击 ctrl 并将其拖到您的视图中,将出现一个弹出窗口,按住 shift 并选择这些约束。

CollectionView Constraints

集合视图约束

Toolbar

工具栏

Check the leading and bottom, by dragging with ctrl in view. And add fixed width and height for toolbar.

通过在视图中使用 ctrl 拖动来检查前导和底部。并为工具栏添加固定宽度和高度。

Toolbar Constraints

工具栏约束

Dealing with scrolling before viewAppears

在 viewAppears 之前处理滚动

-(void)viewWillAppear:(BOOL)animated
{
    [collectionView reloadData];
    [self scrollToLastMessageAnimated:YES];
}

回答by T. Christiansen

You can set the referenceSizeForFooterInSectionfunction in your viewController class:

您可以referenceSizeForFooterInSection在 viewController 类中设置函数:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
    return CGSize(width: view.frame.width, height: 50)
}

Just set the height to your required value.

只需将高度设置为您需要的值。