1

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: enter image description here

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?

1 Answers1

3

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.

Ax1le
  • 6,563
  • 2
  • 14
  • 61