Search code examples
iosuicollectionviewautolayout

How should I set constraint of UICollectionView in iOS


Hi I want to put CollectionView in fullScreen So I set it's constraint all top, buttom, left, right zero with SafeArea But in iPhone 11, When I put many Items, CollectionView Cover the SafeArea with scroll And I can't see last Item fully I can see the Item when I scroll, but it bounce up immediately

When I set HeightConstraint with Constant, same height with SafeArea, It looks perfect So I guess the reason of problem is that Height of SafeArea be changed when CollectionView require Scroll

Any Suggestion for this? If I can, I want to solve it with StroyBoard

Layout

enter image description here

enter image description here

First screen is Auto Layout that I set on Storyboard

And Second One is current Work

I can't see the bottom of Last Cell , I can see if I scroll down, But It Bounce up like that


Solution

  • In your view hierarchy, I noticed a header view is residing for your collection view. Have your tried defining it's height using referenceSizeForHeaderInSection method? You will find that method in collectionViewLayout. Here's reference link :- https://developer.apple.com/documentation/uikit/uicollectionviewdelegateflowlayout/1617702-collectionview