CollectionView AutomaticallyAdjustsScrollViewInsets 无法正常工作

Posted

技术标签:

【中文标题】CollectionView AutomaticallyAdjustsScrollViewInsets 无法正常工作【英文标题】:CollectionView AutomaticallyAdjustsScrollViewInsets not working properly 【发布时间】:2018-06-08 15:19:27 【问题描述】:

我有一个 TabBar,其中包含 CollectionViews。 最近遇到第一个CollectionViews ContentInset顶部为0的问题,但只是最开始。这意味着它不应该在 TopBar 下,当我浏览 TabBar 时,它会修复。

我关注了这个question,但解决方案根本没有做任何事情。他们还建议将我半透明的 TopBar 变成不透明的,我真的想避免这样做。

这里有一张图片来解释更多...... 这是加载内容的方式:

我的集合视图中的项目出现在我的顶部栏下,这意味着 ContentInset 设置为 0 而不是正确地自动调整。 然后,当我通过 tabBar 导航时,它会修复两个选项卡,正如我之前所说的。

我尝试从 CollectionView 中禁用 AutomaticallyAdjustsScrollViewInsets,并根据导航栏和状态栏的大小手动执行此操作。

this.AutomaticallyAdjustsScrollViewInsets = false;
this.CollectionView.ContentInset = new UIEdgeInsets(UIApplication.SharedApplication.StatusBarFrame.Height + this.NavigationController.NavigationBar.Frame.Height, 0, 0, 0);

起初这似乎可行,但是当我在 TabBar 中导航时,ContentInset 修改并向下移动,好像 AutomaticallyAdjustsScrollViewInsets 设置为 false 不起作用。

关于如何在不使导航栏不透明的情况下解决此问题的任何想法?

【问题讨论】:

【参考方案1】:

ios 11 上,如果你想禁用 AutomaticallyAdjustsScrollViewInsets 之前所做的功能,我们应该使用 ContentInsetAdjustmentBehavior。首先在ViewDidLoad()事件中将AutomaticallyAdjustsScrollViewInsets设置为false,以适应较低的iOS版本。然后在 iOS 11+ 上添加以下代码:

public override void ViewWillAppear(bool animated)

    base.ViewWillAppear(animated);

    if (!AutomaticallyAdjustsScrollViewInsets)
    
        if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
        
            MyCollectionView.ContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.Never;
        
    
    else
    
        if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
        
            MyCollectionView.ContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.Automatic;
        
    

最后您可以手动设置 CollectionView 的 ContentInset 以满足您的要求。

【讨论】:

哦!! NICE,找了整整一周的时间,你真是个救星,非常感谢! 很高兴它帮助了你。

以上是关于CollectionView AutomaticallyAdjustsScrollViewInsets 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

CollectionView 中的 CollectionView:使用 didSelectItemAt 执行Segue

为啥 CollectionView 单元格内部其他 collectionView 的大小错误?

CollectionView 单元格出现在 collectionView 之外。

CollectionView 里面的 CollectionView | CollectionViewCell 自动布局错误

当collectionView折叠时隐藏collectionView内容

普通 CollectionView 单元格中的动态 CollectionView 单元格