I have searched StackOverflow for answers to this and have tried different ways of implementing this. I have a collectionView
that has a button that, when pressed, attempts to initialize another collectionView
. Here's the stack trace:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
*** First throw call stack:
(
0 CoreFoundation 0x000000010adb3d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001079b321e objc_exception_throw + 48
2 CoreFoundation 0x000000010ae1d2b5 +[NSException raise:format:] + 197
3 UIKit 0x0000000108eca775 -[UICollectionView initWithFrame:collectionViewLayout:] + 76
4 UIKit 0x0000000108f13abb -[UICollectionViewController _newCollectionViewWithFrame:collectionViewLayout:] + 108
5 UIKit 0x0000000108f12c94 -[UICollectionViewController loadView] + 678
6 UIKit 0x000000010873761c -[UIViewController loadViewIfRequired] + 201
7 UIKit 0x0000000108737e70 -[UIViewController view] + 27
8 UIKit 0x0000000108ff86a4 -[_UIFullscreenPresentationController _setPresentedViewController:] + 87
9 UIKit 0x0000000108712702 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 141
10 UIKit 0x000000010874ae97 -[UIViewController _presentViewController:withAnimationController:completion:] + 3956
11 UIKit 0x000000010874e26b -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 530
12 UIKit 0x000000010874dd51 -[UIViewController presentViewController:animated:completion:] + 179
13 TheAppProject 0x0000000107385842 _TFC17TheAppProject15DailyController12publishedTapfT6senderCSo8UIButton_T_ + 754
14 TheAppProject 0x00000001073859fa _TToFC17TheAppProject15DailyController12publishedTapfT6senderCSo8UIButton_T_ + 58
15 UIKit 0x00000001085978bc -[UIApplication sendAction:to:from:forEvent:] + 83
16 UIKit 0x000000010871dc38 -[UIControl sendAction:to:forEvent:] + 67
17 UIKit 0x000000010871df51 -[UIControl _sendActionsForEvents:withEvent:] + 444
18 UIKit 0x000000010871ce4d -[UIControl touchesEnded:withEvent:] + 668
19 UIKit 0x0000000108605545 -[UIWindow _sendTouchesForEvent:] + 2747
20 UIKit 0x0000000108606c33 -[UIWindow sendEvent:] + 4011
21 UIKit 0x00000001085b39ab -[UIApplication sendEvent:] + 371
22 UIKit 0x0000000108da072d __dispatchPreprocessedEventFromEventQueue + 3248
23 UIKit 0x0000000108d99463 __handleEventQueue + 4879
24 CoreFoundation 0x000000010ad58761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
25 CoreFoundation 0x000000010ad3d98c __CFRunLoopDoSources0 + 556
26 CoreFoundation 0x000000010ad3ce76 __CFRunLoopRun + 918
27 CoreFoundation 0x000000010ad3c884 CFRunLoopRunSpecific + 420
28 GraphicsServices 0x000000010dc1ba6f GSEventRunModal + 161
29 UIKit 0x0000000108595c68 UIApplicationMain + 159
30 TheAppProject 0x0000000107366d8f main + 111
31 libdyld.dylib 0x000000010bd6368d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Here's my code:
private let reuseIdentifier = "cell"
class MyCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let myCell = MyCell()
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.backgroundColor = UIColor.black
collectionView.alwaysBounceVertical = true
collectionView.delegate = self
collectionView.dataSource = self
self.collectionView!.register(MyCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
// MARK: - Layout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 325, height: 630)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 60.0
}
}
Your MyCollectionViewController class is a subclass of UICollectionViewController which has a property of collectionView which is the actual UICollectionView itself. So the separate UICollectionView you create in this line:
let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
is not the same as self.collectionView which is the one provided by the UICollectionViewController itself.
So this is not the source of the error you are getting.
In fact the error you are seeing is relayed to how you are creating and presenting the MyCollectionViewController itself. You don't show the code for how that is created but you are not passing any kind of layout to it. For example to create it in code you would do this:
let layout = UICollectionViewFlowLayout()
let myCollectionVC = MyCollectionViewController(collectionViewLayout: layout)