0

Use UICollectionView and want to show a small number of contents at once without scrolling

For example, if the number of contents is one or more, it can be five. However, if you set the UICollectionView height to 200 in case you have 5 contents when you have 1 content, you have 200 height. How would you like the height to be flexible depending on the number of content?

If the content is five, uicollectionView's height is 200, and if it's one, height is 40

greatsk
  • 23
  • 3
  • 9
  • 1
    Possible duplicate of [UICollectionView - dynamic cell height?](https://stackoverflow.com/questions/28161839/uicollectionview-dynamic-cell-height) – Willjay Nov 29 '17 at 08:58
  • I think you are asking to increase CollectionView height according to number of cells ? like if one cell 200 if 3 it need to be 600 kite this ? – iOS Geek Nov 29 '17 at 09:04
  • @iOS Geek yes That's what I want. – greatsk Nov 30 '17 at 00:27
  • @WeiJay Probably not... but thanks – greatsk Nov 30 '17 at 00:28
  • Possible duplicate of [UICollectionView Self Sizing Cells with Auto Layout](https://stackoverflow.com/questions/25895311/uicollectionview-self-sizing-cells-with-auto-layout) – mfaani Nov 30 '17 at 00:45
  • @Honey No, I want to change the size of the uiCollectionview, not the size of the cell. – greatsk Nov 30 '17 at 01:04
  • gotcha, follow the approach mentioned [here](https://stackoverflow.com/a/47557592/5175709). It's for tableViews, but I'm assuming the logic very much applies. It will return an intrinsicsize and then don't place any constraints for the collectionView's height so it would apply its height using the intrinsic size. – mfaani Nov 30 '17 at 01:08

4 Answers4

1

You can do something like below to achieve this:

Take a outlet of height constraint to your collection view and then change the height as desired for example

let height: CGFloat = (number of rows or content to show) * (heigh of row) //Calculation can differ according to needs
collectionViewHeightConstraint.constant = height
self.view.layoutIfNeeded()

I hope this may help you :)

iOS_MIB
  • 1,885
  • 13
  • 24
0

You can disable scroll.

self.collectionView.scrollEnabled = NO;
mhmtkrgz
  • 86
  • 5
0

You can do this by using this:

In viewWillAppear method update UICollectionView depend on content count

 let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
 layout.itemSize.height = content.count * 40
 self.collectionViewLayout = layout

Here I set an approximate value for each count. In this process, if the content count is 1 then collection view hight will be 40 and if content counts 5 then collection view height will be 200.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
0

Two ways can be implemented -- Need to provide zero height or a height constraint to collectionView in storyboard

and now in View will Appear just try these two simple steps

  1. Get Contentsize height and Set frame height

    HotDealsCollectionView.frame.size.height = HotDealsCollectionView.contentSize.height

  2. Get Height On base of number of cells

    HotDealsCollectionView.frame.size.height = (Array.count * Static height if you provided)

Can call LayoutsIfNeeded() At end of both Options

Prefer First As Second will work if you are showing one in a row I mean Vertical CollectionView whereas in first option you will get its ContentSize height which you are actually looking for

NOTE: If in case your result is not getting expected like after adopting these options Frame size is not increased the just add some static height value at end like

HotDealsCollectionView.frame.size.height = HotDealsCollectionView.contentSize.height + 50

Hope it Helps

iOS Geek
  • 4,825
  • 1
  • 9
  • 30