2

I have embedded a collection view in another view and disabled the collection view's scrolling ability, what i want to achieve is similar to Instagram's profile tab. However, I cannot figure out how should I set the height of the collection view in this case since the number of cells are dynamic.

I tried searching different solutions but most results are on changing the cells dynamically but not the collection view height itself. Is there any default/standard solutions for that?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3402639
  • 228
  • 1
  • 3
  • 11

5 Answers5

9

Set the width you want the collection view to have (hopefully that is static), and request a layout:

collectionView.frame = CGRectMake(0., 0., width, 0.);
[collectionView layoutIfNeeded];

The calculated height of the collectionView will then be available at:

collectionView.contentSize.height
Murillo
  • 1,043
  • 8
  • 9
  • i want to set the dynamic height of collection view, depend on the number of cells ,please tell me – Ravi Ojha Oct 26 '15 at 10:54
  • @RaviJSS: Please check my answer below if you are working with swift. If you are working in objective-c then go through http://stackoverflow.com/a/28378625/6325474. This answer will help you a lot. – Er. Vihar Apr 19 '17 at 05:44
4

For Swift please follow below steps:

Declare a CGFloat variable in declaration section:

var height : CGFloat!

At viewDidAppear you can get it by:

height = self.myCollectionView.collectionViewLayout.collectionViewContentSize().height

Maybe when you reload data then need to calculate a new height with new data then you can get it by: addObserver to listen when your CollectionView finished reload data at viewWillAppear:

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        ....
        ....
        self.shapeCollectionView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.Old, context: nil)
    }

Then add bellow function to get new height or do anything after collectionview finished reload:

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        let newHeight : CGFloat = self.myCollectionView.collectionViewLayout.collectionViewContentSize().height

        var frame : CGRect! = self.myCollectionView.frame
        frame.size.height = newHeight

        self.myCollectionView.frame = frame
    }

And don't forget to remove observer:

self.myCollectionView.removeObserver(self, forKeyPath: "contentSize")

I hope this will help you to solve your issue in swift.

Er. Vihar
  • 1,495
  • 1
  • 15
  • 29
2

1) you need to set height constraint to collectionview in storyboard or xibs

2) make outlet of heightConstraint

3) and use this code while relaod collectionview

collectionViewHeightConstaint.constant = collectionView.contentSize.height
Narendra Jagne
  • 436
  • 3
  • 8
1

In your ViewController, set the frame of the collectionView. For example:

    self.collectionView.frame = CGRectMake(0, 0, 100, 200);
John
  • 2,672
  • 2
  • 23
  • 29
  • the height of the collection view is dynamic, depending the number of posts the user created, just like instagram, so i can't make it to constant.... – user3402639 Mar 17 '14 at 05:48
  • Usually there is a dynamic number of cells. But if you really want a dynamic height of collectionView, you can set its frame whenever your datasource's array.count changes. – John Mar 17 '14 at 11:05
1

Based on the fact you know the height of the cells and you know the number of cells on the screen. I would...

self.collectionView.frame = CGRectMake(0, 0, DEFINED_WIDTH, DEFINED_CELL_HEIGHT*Number of Cells);

or have I miss understood the question?

Spanners
  • 366
  • 2
  • 20