D4rWiNS
D4rWiNS

Reputation: 2615

Change UICollectionView size without viewDidLayoutSubviews

I have a CollectionView(Vertical) inside of a ScrollView to make it scrollable in both directions.

The user need to select the seat in the room, and the room can have any number of columns and rows, I need to put in order the seats

If I put a value ( 800 for example) of width on the collection view on viewDidLayoutSubviews it works

- (void)viewDidLayoutSubviews {
self.collectionView.frame = CGRectMake(0, 0, 800, self.collectionView.frame.size.height);
self.scrollView.contentSize = self.collectionView.frame.size;

}

The problem is, that I load the seats in a Async task, and If I use

self.collectionView.frame = CGRectMake(0, 0, 113*columns, 113*rows);
self.scrollView.contentSize = self.collectionView.frame.size;

after load the seats It doesnt work.

I cant put the size in viewDidLayoutSubviews because I dont know still how many seats !

How I can do it after load the data?

Upvotes: 0

Views: 2418

Answers (1)

Cristik
Cristik

Reputation: 32873

You can call setNeedsLayout or layoutIfNeeded after receiving the data. This will ask the view to update update the layout of subviews, which will trigger a call to viewDidLayoutSubviews.

Note that you'll need to update the viewDidLayoutSubviews method to take into account the new values for columns and rows.

The difference between the two is that layoutIfNeeded has an immediate effect, while setNeedsLayout will trigger the re-layout in the next update cycle. Performance-wise setNeedsLayout is preferred, at it also coalesces multiple layout invalidations, however depending if you have animations or not, layoutIfNeeded might be more preferable.

Upvotes: 2

Related Questions