UICollectionView view and constraints to make cells fit

时间:2015-06-30 13:42:30

标签: ios iphone swift ipad interface-builder

After playing a bit these days with UICollectionView, I think I understand how it works but, there's something I'm missing.

I have 8 views added to the UICollectionView and they do not jump to the next row when they don't fit in the screen. It seems that the UICollectionView is bigger than the screen. I had to use constraints on the UICollectionView to make it same width and height of the parent view.

Here some screenshots:

Without Constraints:

Without constraints:

With Constraints:

With constraints

Could anyone explain this behaviour?

1 个答案:

答案 0 :(得分:0)

If you don't set constraints on a view in Storyboard, it will retain its dimensions as specified in the Storyboard - it will not automatically resize with superview. So the fact that the root view of UIViewCntroller was resized to fit the screen does not affect size of UICollectionView.

As you have been creating your UICollectionView in universal size it ended up much bigger that actual screen.


To answer your follow-up questions in the comments:

  1. At the runtime will persist constants of it's width and height, not relation to the size of it's parent (Unless you specify otherwise).

  2. When UIViewController is shown, (in most cases) it's root view is resized to fit the screen. However, it's children will remain unchanged, unless you specify otherwise.

  3. By universal size I meant the size of UIViewController in the storyboard as you specify in Attribute Inspector -> Size -> Freeform, which defaults to 600x600. I suppose "freeform" would be more correct than "universal".