UICVCompositionalLayout using StoryBoard and Obj-C

Trying to get Compositional Layout working (in Obj-C: legacy code base, legacy dev) with CollView in StoryBoard. Keep getting this error:
> 'Invalid section definition. Please specify a valid section definition when content is to be rendered for a section. This is a client error.'
Looking at the Code Sample I cannot see what I'm doing differently. Here is the code.
Code Block objective-c
@implementation TemplatePickerCompLayout
- (UICollectionViewCompositionalLayout*)initWithCoder:(NSCoder *)coder{
self = [super initWithCoder:coder];
self.collectionView.collectionViewLayout = [self createStandardCompLayout];
return self;
}
- (UICollectionViewLayout*)createStandardCompLayout{
NSCollectionLayoutSize* itemSize = [NSCollectionLayoutSize sizeWithWidthDimension:[NSCollectionLayoutDimension fractionalWidthDimension:1.0]
heightDimension:[NSCollectionLayoutDimension fractionalHeightDimension:1.0]];
NSCollectionLayoutItem* item = [NSCollectionLayoutItem itemWithLayoutSize:itemSize];
item.contentInsets = NSDirectionalEdgeInsetsMake(5.0f, 5.0f, 5.0f, 5.0f);
NSCollectionLayoutSize* groupSize = [NSCollectionLayoutSize sizeWithWidthDimension:[NSCollectionLayoutDimension fractionalWidthDimension:0.80] heightDimension:[NSCollectionLayoutDimension absoluteDimension:120.0f]];
NSCollectionLayoutGroup* group = [NSCollectionLayoutGroup horizontalGroupWithLayoutSize:groupSize subitem:item count:1];
group.contentInsets  = NSDirectionalEdgeInsetsMake(10.0f, 30.0f, 10.0f, 30.0f);
NSLog(@"Group:\n%@",group.visualDescription);
UICollectionViewLayout* rtnLayout = [[UICollectionViewCompositionalLayout alloc]initWithSectionProvider:^NSCollectionLayoutSection * _Nullable(NSInteger section, id<NSCollectionLayoutEnvironment> _Nonnull env) {
NSLog(@"env:%@ [%zd]",env,section)
NSCollectionLayoutSection* sectionDef = nil;
if (section == 0) {
sectionDef = [NSCollectionLayoutSection sectionWithGroup:group];
sectionDef.contentInsets = NSDirectionalEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);
} else {
sectionDef = [NSCollectionLayoutSection sectionWithGroup:group];
sectionDef.contentInsets = NSDirectionalEdgeInsetsMake(10.0f, 50.0f, 10.0f, 50.0f);
}
return sectionDef;
}];
return rtnLayout;
}
@end




Replies

This exception indicates that you are returning nil from the section provider even though there are sections in the collection view.

It looks like you are creating the collection view from interface builder. Which layout are you using in interface builder? Are there maybe any other collection views or different compositional layouts involved that might trigger this exception?
Correct, trying to use IB to instantiate the CompLayout object. I’ve set the Layout type to Custom, and the Class to my custom CompLayout class.
InitWithCoder doesn’t seem to work.
Thx