0

I have seen a similar question asked here about creating collection views programmatically but not with IB or nibs or xibs or whatever I am supposed to call it.

Its my understanding that when using a XIB the objects inside the XIB get instantiated automatically by IB. Therefore, it makes sense that for another collectionView I am creating with a XIB it works without programmatically instantiating a flow layout object. However, this second view is crashing with the error:

'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

My first thought was to alloc/init a new flow layout and add it to the collectionViews layout object as in self.collectionView.collectionViewLayout = newLayout. (I'm in a collection View Controller). However, this property seems to be read-only. Because I'm using a XIB I assume xcode is somehow doing the layout behind the scenes but that doesn't help with figuring out whats going wrong.

AvidLearner
  • 4,123
  • 5
  • 35
  • 48

2 Answers2

0

My first thought was to alloc/init a new flow layout and add it to the collectionViews layout object

Probably the opposite order: first initialize the FlowLayout instance then initialize the CollectionView with frame and layout

vadian
  • 274,689
  • 30
  • 353
  • 361
  • but the collection view is being initialized by the xib. If i go to set the layout property of the collection view in the viewDidLoad of my collectionView controller it is read-only. – Alexander Bollbach Jul 07 '15 at 14:52
  • the documentation definitely says that a CollectionView must be initialized with a non-nil FlowLayout. Either create both objects in IB or both programatically – vadian Jul 07 '15 at 14:59
  • i have two view controllers both with a XIB file. The parent view controller launches fine but the child view crashes because of a nill layout. I've checked every page in the utilities and both XIBs are identical. Somehow the working view controller's XIB is properly setting up a layout and the child view controller isn't. Running a project level search for "layout" yields no results so I know the layouts are being instantiated and added to the collection views behind the scenes. – Alexander Bollbach Jul 07 '15 at 15:12
0

If anyone stumbles across this I think I've got the solution. Assuming you have a collection view layout set in your XIB, I've confirmed with Apple that there is a bug with calling UICollectionViewController.init() and UICollectionViewController.init(nibName: nil, bundle: nil) that causes the collection view's layout to not be set.

Workaround:

To work around this use UICollectionViewController.init(nibName: "NameOfMyXIB", bundle: nil) and it will appropriately set the collection view's layout.

InkGolem
  • 2,662
  • 1
  • 15
  • 22