0

I've got a XIB named 'ImageViewController.xib" which contains a collection view. I've set up my desired collection view layout on this collection view inside the xib.

When trying to run my app I get the error message:

'UICollectionView must be initialized with a non-nil layout parameter'

I'm using this inside ImageViewController:

-(id) initWithImages:(NSArray *)imagesUrls
{
    if (self = [super initWithCollectionViewLayout:self.collectionViewLayout]) { 
        self.imageUrls = [[NSArray alloc] initWithArray:imagesUrls];
    }

    [self.collectionView registerNib:[UINib nibWithNibName:@"ImageCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    return self;
}

I just want to use the layout I have set up in the XIB and not make one programatically.

ritch
  • 1,760
  • 14
  • 37
  • 65

2 Answers2

0

You can add the collectionViewLayout to the collectionView in the viewDidLoad.

Create your collectionViewLayout eg:

UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flowLayout.minimumLineSpacing = 5;
flowLayout.minimumInteritemSpacing = 5;
flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);

Then we set it as the flow layout of the collectionView

_collectionView.collectionViewLayout = flowLayout;

I found this answer on a good tutorial which can be found here

Hopefully this helps

sam_smith
  • 6,023
  • 3
  • 43
  • 60
-1

Try adding this in your view did Load method act its what I did to get rid of it

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(100, 100);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];

this worked for me…you see what the error is telling you that the layout should not be nil, the Layout's frame is set relative to its superview, the layout is passed to the parent class during initialization, check if your CollectionViewLayout is "Null"

Geet
  • 2,427
  • 2
  • 21
  • 39
  • This doesn't change anything. What does my custom cell have to do with the collection view layout? – ritch Jul 24 '14 at 12:55
  • 1
    I don't want to make a layout programatically, I want to use the one I have configured in the xib file using the interface builder. – ritch Jul 24 '14 at 13:41
  • The problem is here, if (self = [super initWithCollectionViewLayout:self.collectionViewLayout]) make sure that self.collectionViewLayout is not nil – Geet Jul 24 '14 at 13:46