8

I'm trying to resize a UICollectionView height by setting it to 0 when the view controller is loaded, then increasing its size with an animation when a button is pressed. I've tried a few different things but it doesn't change in size at all. Here's all the different things I've tried to change its height to 0:

CGRect bounds = [self.collectionView bounds];
[self.collectionView setBounds:CGRectMake(bounds.origin.x,
                                          bounds.origin.y,
                                          bounds.size.width,
                                          bounds.size.height - 100)];

....

CGRect frame = [self.collectionView frame];
[self.collectionView setFrame:CGRectMake(frame.origin.x,
                               frame.origin.y,
                               frame.size.width,
                               frame.size.height - 100)];

....

CGRect frame = self.collectionView.frame;
frame.size.height -= 100;
self.collectionView.frame = frame;

....

 self.collectionView.clipsToBounds = YES;
 self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
Jay Slupesky
  • 1,893
  • 13
  • 14
Neil Faulkner
  • 526
  • 1
  • 4
  • 22
  • Is `self.collectionView` actually set to something? Are you using AutoLayout? – mattjgalloway Oct 22 '12 at 19:06
  • i set the collection view as a outlet. Yes AutoLayout is on. – Neil Faulkner Oct 22 '12 at 20:20
  • 5
    If you're using AutoLayout then you're going to have to change the constraints rather than directly changing the frame. You'll want to set a height constraint on the collection view. – mattjgalloway Oct 22 '12 at 20:21
  • Ah ok, havent had much experience with AutoLayout. So what do i have to change, the Height constraint is set at 100 at the moment. – Neil Faulkner Oct 22 '12 at 20:37
  • Then change it to 0, assuming you want it to be 0? Then switch it back to 100 when you want it back at 100 again. – mattjgalloway Oct 22 '12 at 20:38
  • I want it set as 0 when the ViewController loads, then when a button is pressed, it triggers a animation of the height increasing from 0 - 100, revealing to the CollectionView. – Neil Faulkner Oct 22 '12 at 20:43
  • Re-reading your question I need to ask this - you realise that `frame.size.height - 100` will take the current value and subtract 100, right? It sounds like perhaps you are thinking it means `frame.size.height` to `100`. – mattjgalloway Oct 22 '12 at 21:32
  • Yeah, the code above is in viewDidLoad, so it sets the CollectionView height to zero, hence why i take away 100 from current height. When the user hits a button it + 100 to increase the height revealing the CollectionView to the user. The collection view holds a menu, i want this menu to be able to be hidden when not being used. – Neil Faulkner Oct 22 '12 at 21:49
  • Ok good. Yep so if you're using AutoLayout then you need to change the layout constraint rather than setting the frame directly. – mattjgalloway Oct 22 '12 at 22:36
  • How do i do this programatically? – Neil Faulkner Oct 23 '12 at 18:23

2 Answers2

4

If you are using Interface Builder for UICollectionView initialisation, switch off "Use Autolayout" from the File Inspector in the xib file where the your CollectionView is created. Then you can change the height with setFrame or setBounds methods.

2

You can avoid disabling Autolayout by creating an outlet for the height constraint and then adjusting the constraint's constant in code.

Outlet

@IBOutlet weak var collectionViewVerticalConstraint: NSLayoutConstraint!

Adjustment

collectionViewVerticalConstraint.constant = 0
sheepgobeep
  • 743
  • 11
  • 25