Resize UICollectionView Height
Asked Answered
H

2

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;
Hellenist answered 22/10, 2012 at 18:32 Comment(10)
Is self.collectionView actually set to something? Are you using AutoLayout?Hylomorphism
i set the collection view as a outlet. Yes AutoLayout is on.Hellenist
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.Hylomorphism
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.Hellenist
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.Hylomorphism
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.Hellenist
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.Hylomorphism
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.Hellenist
Ok good. Yep so if you're using AutoLayout then you need to change the layout constraint rather than setting the frame directly.Hylomorphism
How do i do this programatically?Hellenist
T
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.

Treadle answered 17/12, 2013 at 15:6 Comment(3)
It working fine but we will loose AutoLayout throughout the application.Cupule
You could still use the autoResizing capabilitiesMonaco
There is no way to achieve this with AutoLayout enabled?Nagle
M
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
Martine answered 25/8, 2015 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.