10

I'm using a storyboard for my project and I want to have a scene that's a UICollectionViewController sub class, who's UICollectionView uses a UICollectionViewLayout subclass.

I've set this up in a storyboard, and I'm trying to change the "Custom Class" of the collection view's layout instance.

Here's a screen grab of the layout object that I have selected in the storyboard:

Storyboard object tree with <code>UICollectionViewLayout</code> selected

Here's a screen grab of storyboard's "Identity" inspector pane showing the "Custom Class" field that I am changing (it's at the top):

Storyboard Identity inspector showing "Custom Class"

When I try to change the "Custom Class", I can change type stuff in, but after saving, the it reverts back to being the default UICollectionViewFlowLayout. If I run the project, the default UICollectionViewFlowLayout gets used even though it's not the custom class I have set.

How can I set a custom layout class, please?

Benjohn
  • 13,228
  • 9
  • 65
  • 127

1 Answers1

13

The approach you've used would usually let you change the class of an instance in a storyboard. However, for a UICollectionView, you need to do something slightly different.

Instead of selecting the UICollectionViewLayout instance itself and adjusting its custom class parameter, select the UICollectionView instance, as show in this screen grab:

Storyboard object tree with <code>UICollectionView</code> instance selected

Now, in the storyboard inspector, select the "Attributes" inspector pane for the UICollectionView instance. You'll see something like this:

Storyboard Attributes inspector with "Flow" selected

You need to change the "Layout" field combo box so that instead of "Flow" being selected, "Custom" is selected. A text field will then be appear to set your UICollectionViewLayout custom subclass, like this:

Storyboard Attributes inspector with "Custom" selected

Benjohn
  • 13,228
  • 9
  • 65
  • 127
  • Thank you so much for this clear explanation. This inconsistency in the way custom classes are selected for layouts stumped me 2 days. :( – motionpotion Jul 08 '14 at 13:16
  • :-) I'm glad you've got the solution now, but worry you had to search a few days – it took me a while too! I've got the habit of checking S.O. if anything takes me more than a few minutes to work out. Then at least you know if this is something only you've found an issue with, or whether there's a known solution! – Benjohn Jul 08 '14 at 14:22