title: Dynamically Set ContentSize of UICollectionView
body: <p>I have a <code>UICollectionView</code> that has been created programatically. Upon creation of the collection view, I would like to dynamically define the collection view height based on the number of cells that it must hold. I do not want to enable scrolling of the collection view itself, rather this collection view is being added as a subview to a view that is contained inside a vertical <code>UIScrollView</code>.</p>
  
  <p>For example, if the <code>UICollectionView</code> has 10 <code>UICollectionViewCells</code>. It might have a height of 200.0f, but if it has 20 cells it might have a height of 300.0f, and so on. </p>
  
  <p>I have attempted to accomplish this by following the Apple docs <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionViewLayout_class/Reference/Reference.html" rel="nofollow noreferrer">here</a>, overriding the <code>collectionViewContentSize</code> method. </p>
  
  <p>Although this method does return a valid <code>CGSize</code> when it is called, when I instantiate the collection view, its <code>frame</code> is always set to zero.</p>
  
  <p>Here is what I have done so far:</p>
  
  <pre><code>//subclass UICollectionViewFlowLayout
  
  @interface LabelLayout : UICollectionViewFlowLayout
  
  @property (nonatomic, assign) NSInteger cellCount;
  @property (nonatomic) UIEdgeInsets sectionInset;
  @property (nonatomic) CGSize itemSize;
  @property (nonatomic) CGFloat minimumLineSpacing;
  @property (nonatomic) CGFloat minimumInteritemSpacing;
  
  @end
  
  - (id)init
  {
     self = [super init];
      if (self) {
          [self setup];
      }
  
  return self;
  }
  
  -(void)prepareLayout
  {
      [super prepareLayout];
      _cellCount = [[self collectionView] numberOfItemsInSection:0];
  }
  
  - (void)setup
  {
      self.sectionInset = UIEdgeInsetsMake(10.0f, 0.0f, 10.0f, 0.0f);
      self.itemSize = CGSizeMake(245.0f, 45.0f);
      self.minimumLineSpacing = 10.0f;
      self.minimumInteritemSpacing = 20.0f;
  }
  
  - (CGSize)collectionViewContentSize
  {
     CGFloat collectionViewWidth = 550;
     CGFloat topMargin = 10;
     CGFloat bottomMargin = 10;
     CGFloat collectionViewHeight = (self.cellCount * (self.itemSize.height +    
     self.minimumLineSpacing*2)) + topMargin + bottomMargin;
  
     //THIS RETURNS A VALID, REASONABLE SIZE, but the collection view frame never gets set with it!
     return CGSizeMake(collectionViewWidth, collectionViewHeight);
   }
  
  //create collectionView in viewController
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
      [self makeLabels]; //determines the number of cells
  
      LabelLayout *layout = [[LabelLayout alloc]init];
      self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];   
      self.collectionView.backgroundColor = [UIColor redColor];
  
      self.collectionView.dataSource = self;
      self.collectionView.delegate = self;
  
      [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
      [self.collectionView reloadData];
      [self.view addSubview:self.collectionView];
  }
  </code></pre>
  
  <p>Additionally, when I explicitly define a static frame for the <code>UIcollectionView</code>, it is created as expected, so I know that my only issue is in using the <code>collectionViewContentSize</code> method. </p>
  
  <p>So my question is, how can I dynamically set the height of my <code>UICollectionView</code>?</p>