Writing this question as my last resort, stuck on this for several days. I am trying to implement size cells for a UICollectionView using FlowLayout . Everything seems to work fine until I reladData() or insertItemsAtIndexPaths() , being at the bottom of the collection.
This is my cell:
class FooFoo: UICollectionViewCell { var label: UILabel! var text: String! { didSet { label.text = text } } override init(frame: CGRect) { super.init(frame: frame) setupView() setupConstraints() } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setupView() { self.contentView.backgroundColor = UIColor.yellowColor() label = ({ let view = UILabel() view.setTranslatesAutoresizingMaskIntoConstraints(false) self.contentView.addSubview(view) return view })() } func setupConstraints() { self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-100-[childView]-100-|", options: nil, metrics: nil, views: ["childView": label])) self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-100-[childView]-100-|", options: nil, metrics: nil, views: ["childView": label])) }
}
This is the definition of CollectionView :
collectionView = ({ let layout = UICollectionViewFlowLayout() layout.minimumLineSpacing = 1 layout.estimatedItemSize = CGSize(width: 300, height: 300) let view = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) view.dataSource = self view.delegate = self view.registerClass(FooFoo.self, forCellWithReuseIdentifier: "Cell") view.backgroundColor = UIColor.clearColor() self.view.addSubview(view) return view })()
This is dataSource :
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return items.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! FooFoo cell.text = "FooBar: \(indexPath.item)" return cell }
After I extracted additional elements, I tried both to insert a new array of indices and reload the data, and the result is the same - a broken layout.
I also tried replacing estimatedItemSize with itemSize , and it really worked very well, so I'm pretty sure that this is because the cells are for self-calibration.
This is the result:

swift uicollectionview uicollectionviewlayout flowlayout collectionview
Katafalkas
source share