6

I have an app that uses a Nav Contoller as it's initial VC, which then has a root UIViewContoller that contains a UIView at the top half, and a UIContainerView at the bottom. In the UIContanerView, I'm embedding a working UICollectionView that contains image buttons that segue to detail views.

The problem is that white space now shows up at the top of the UICollectionView. Given this is around 64 pixels high, it appears to be a ghosting of a Nav Bar 44px + Status Bar 20px = 64.

Extra space of around 64px in my UICollectionView (I set it gray)

And if I scroll up everything looks fine and works as expected, and it also allows me to show you what I expected the layout to look like upon launch:

What I expected the launch layout to look like

A snippet of my storyboard is below if that helps:

Storyboard

Dan Coughlin
  • 1,104
  • 10
  • 14
  • It's been a while since I had this problem and I don't exactly remember the resolution :-(. IIRC The issue is that the container view is inside of a navigation view and the operating system is trying to be very helpful by moving the content down by the height of the navigation view. If I remember correctly one thing you can check is the container view's "layoutMargins". I think you'll find the top margin set and you can try to clear that. – Scott Thompson Apr 02 '16 at 03:46
  • check this ... http://stackoverflow.com/questions/23786198/uicollectionview-how-can-i-remove-the-space-on-top-first-cells-row – Bhavin Bhadani Apr 02 '16 at 04:34
  • Thanks, let me to the one line answer: `automaticallyAdjustsScrollViewInsets = false` – Dan Coughlin Apr 02 '16 at 05:09

1 Answers1

10

yes, that could be because child view controller embedded in container view gets the impression, that it is a direct child of UINavigationController, which in turn make collectionView leave top 64 pt insets.

TO solve this problem,In your child view controller interface builder, unmark adjust scrollView insets

In your child view controller interface builder, unmark adjust scrollView insets

This should solve your problem

UPDATE

As Dan suggested, we can also fix it programatically, by calling

automaticallyAdjustsScrollViewInsets = false

in viewDidLoad() of your UIViewController

gunjot singh
  • 2,578
  • 20
  • 28
  • 1
    That is it, and matches the answer I found from the link @Scott Thompson posted above. I actually placed the one line in the viewDidLoad() so it would be less "hidden" when I come back months from now... ;o) `automaticallyAdjustsScrollViewInsets = false` – Dan Coughlin Apr 02 '16 at 05:08
  • It appears that both container controller and child controller has to be unchecked to make it works. – Terence Mar 07 '17 at 14:43
  • It is only needed for the controller that contains scrollView in it! – gunjot singh Mar 07 '17 at 15:04