简体   繁体   中英

UICollectionView view and constraints to make cells fit

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:

无限制:

With Constraints:

有约束

Could anyone explain this behaviour?

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM