ios – UICollectionViewController以非零布局参数错误编程崩溃

前端之家收集整理的这篇文章主要介绍了ios – UICollectionViewController以非零布局参数错误编程崩溃前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在以编程方式创建一个UICollectionViewController.不,我不想像每个教程那样使用IB和Storyboard.我只想使用普通代码.

这是我在viewDidLoad中的内容

  1. // UICollectionView
  2. UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
  3. [aFlowLayout setItemSize:CGSizeMake(200,140)];
  4. [aFlowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
  5. self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:aFlowLayout];
  6. [self.collectionView setDelegate:self];
  7. [self.collectionView setDataSource:self];
  8. self.collectionView.backgroundColor = [UIColor clearColor];
  9. [self.collectionView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth];
  10. [self.collectionView registerClass:[PUCImageGridCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];

我已经实现了所需的委托方法,但仍然会收到此错误

  1. Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: 'UICollectionView must be initialized with a non-nil layout parameter'

解决方法

这是我用来初始化 UICollectionViewController
  1. - (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout

您不需要实例化集合视图,控制器将为您执行此操作.调用此框后设置框架,或者您可以覆盖并设置内部框架:

  1. - (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout{
  2. self = [super initWithCollectionViewLayout:layout];
  3. if (self) {
  4. // additional setup here if required.
  5. }
  6. return self;
  7. }

我更喜欢使用autolayout约束.

猜你在找的iOS相关文章