CollectionView自动调整ScrollViewInsets无法正常工作(CollectionView AutomaticallyAdjustsScrollViewInsets not working properly)

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

我遵循这个问题 ,但解决方案根本就没有做任何事情。 他们还建议将我的半透明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不起作用。

任何想法如何解决这个问题,而不会导致navbar不透明?

I got a TabBar which contains CollectionViews in it. Recently I've encountered the problem that the first CollectionViews ContentInset is 0 on the top, but only at first. Which means it is under the TopBar when it shouldn't and when I navigate through the TabBar, it fixes.

I followed this question, but the solutions simply don't do anything. They also suggest to turn my translucid TopBar opaque, and I really want to avoid doing that.

Here's an image to explain a little bit more... This is how the content is loading:

The items from my collection view appear under my top bar, which means ContentInset sets to 0 instead of correctly automatically adjusting. Then when I navigate trough the tabBar, it fixes on both tabs, as I said earlier.

I tried disabling the AutomaticallyAdjustsScrollViewInsets from the CollectionView, and doing it manually considering the size of the navbar and the status bar.

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

And this seemed to work at first, but then when I navigate through the TabBar, the ContentInset modifies and it moves down as if the AutomaticallyAdjustsScrollViewInsets set to false didn't work.

Any ideas on how to solve this problem without turnning the navbar opaque?

最满意答案

在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以适应您的请求。

On ios 11, if you want to disable the feature what AutomaticallyAdjustsScrollViewInsets did before, we should use ContentInsetAdjustmentBehavior. Firstly set AutomaticallyAdjustsScrollViewInsets to false in the ViewDidLoad() event to adapt lower iOS versions. Then on iOS 11+ add the code below:

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; } } }

At last you can set the CollectionView's ContentInset manually to fit your request.

更多推荐