I have a UICollectionView with a button for creating cells that should appear in the order they were created (1,2,3,4 wide and right, as space allows). Presentations of the text are limited by a flexible width to fill the cell. The cell size depends on the device and rotation to provide 1, 2, 3 or 4 cells per row as follows:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { var size = collectionView.bounds.size // size of collection view switch size.width { case 0...450: println(size.width) return CGSize(width: (size.width - 10), height: 145) case 451..<768: println(size.width) return CGSize(width: ((size.width - 10) / 2), height: 145) case 768: println(size.width) return CGSize(width: ((size.width - 10) / 3), height: 145) default: println(size.width) return CGSize(width: 248, height: 150) // in case there is no size } }
With left and right inserts set to 0, this kind of work - but only if I rotate the device and add a cell. More details:
- Simulated iPhone 5, if cells are added to the portrait, they are displayed below each other, as expected, but when the device is rotated, the cells remain in a vertical column instead of two cells in a row. If I add a cell to the terrain, the cells then arrange the two cells in a row, which is correct (except that you need to add a cell for this to happen).
- Simulated iPad, if cells are added in the portrait, instead of three, only two cells are added to the row with space for the third cell in between.
- Continuing to work with the iPad, if the device is rotated, and not four, only three cells with a space between them appear on each line. Like the iPhone, if another cell is added in landscape mode, four cells are displayed on each line as expected, and if the device is rotated back to the portrait, three cells are located on the line, which is correct (except that you need to rotate the device, add cell and rotate it back).

I call reloadData when adding cells, and if I understand correctly, I will not need to overwrite the reboot when the device rotates (it is called automatically). Why do I need to rotate the device to landscape and add a cell for the correct layout in both orientations?
As requested, the source code version for VC is cut here. With a simple cell (244 * 150 with a text box of 228) and a button in the footer this should repeat the problem:
import UIKit class CountCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { private var reuseIdentifier = "CountCell" var cells = 1 override func viewDidLoad() { super.viewDidLoad() self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") } @IBAction func addCount(sender: AnyObject) { cells += 1 self.collectionView?.reloadData() } override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return cells } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell return cell } override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { switch kind
}