Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding a Collection View inside a Container View - extra white-space at top

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

like image 234
Dan Coughlin Avatar asked Apr 02 '16 03:04

Dan Coughlin


People also ask

How to set collection view in swift?

Add a CollectionView by pressing command shift L to open the storyboard widget window. Drag the collectionView onto the main view controller. Add constraints to the UICollectionView widget to ensure that the widget fills the screen on all devices. The foundation is now set up in the storyboard.

What is CollectionView?

A collection view manages an ordered set of content, such as the grid of photos in the Photos app, and presents it visually. Collection views are a collaboration between many different objects, including: Cells. A cell provides the visual representation for each piece of your content.

How to use collection view in swift ios?

Open the storyboard file in which you want to add the collection view. Press Cmd+Shift+L to open the Xcode Library and search for collection view. Select and drag the Collection View option in your screen. You will see a new blank collection view added on your screen and in left panel under Safe Area.


1 Answers

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

like image 152
gunjot singh Avatar answered Sep 28 '22 17:09

gunjot singh