UICollectionViewController以非零布局参数错误编程崩溃
我正在编程创build一个UICollectionViewController
。 不,我不想使用IB和Storyboard,比如每个教程。 我只是想用简单的代码。
这是我在我的viewDidLoad
有什么:
// UICollectionView UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init]; [aFlowLayout setItemSize:CGSizeMake(200, 140)]; [aFlowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:aFlowLayout]; [self.collectionView setDelegate:self]; [self.collectionView setDataSource:self]; self.collectionView.backgroundColor = [UIColor clearColor]; [self.collectionView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth]; [self.collectionView registerClass:[PUCImageGridCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
我已经实现了所需的委托方法,我仍然得到这个错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
这是我用来初始化一个UICollectionViewController :
- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
你不需要实例化collectionView控制器会为你做。 调用后设置框架,或者你可以覆盖和设置里面的框架:
- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout{ self = [super initWithCollectionViewLayout:layout]; if (self) { // additional setup here if required. } return self; }
我更喜欢使用自动布局约束。