Wednesday, 21 August 2013

How can I programmatically change the size of a UICollectionView?

How can I programmatically change the size of a UICollectionView?

I created a UICollectionView in the interface builder. I referenced it with
@property (strong, nonatomic) IBOutlet UICollectionView *contactList;
in my .h file and @synthesize contactList; in my .m file.
I tried to implement a slightly different layout in portrait and landscape
using the following code:
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
if (orientation == UIInterfaceOrientationPortrait || orientation ==
UIInterfaceOrientationPortraitUpsideDown) {
UICollectionViewFlowLayout *flow =
(UICollectionViewFlowLayout*)contactList.collectionViewLayout;
flow.sectionInset = UIEdgeInsetsMake(48, 80, 48, 0);
flow.minimumLineSpacing = 80;
} else if (orientation == UIInterfaceOrientationLandscapeLeft ||
orientation == UIInterfaceOrientationLandscapeRight) {
UICollectionViewFlowLayout *flow =
(UICollectionViewFlowLayout*)contactList.collectionViewLayout;
flow.sectionInset = UIEdgeInsetsMake(48, 64, 48, 0);
flow.minimumLineSpacing = 64;
}
}
This works prefectly, but I also want to change the size (in landscape I
have a 2*3 grid per page and in portrait it's a 3*2 grid). I tried setting
the size like this:
CGRect frame = [contactList frame];
frame.size.width = 960;
[contactList setFrame:frame];
But it won't update. There is no error, but the view just isn't updated.
It just stays at the same size as before. What do I have to do to update
the view?

No comments:

Post a Comment