Home > front end >  'UICollectionView must be initialized with a non-nil layout parameter' bug
'UICollectionView must be initialized with a non-nil layout parameter' bug

Time:02-15

I'm initializing a UICollectionViewController in SceneDelegate:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
      
    let window = UIWindow(windowScene: windowScene)
    let tabBarVC = AppTabBarController()
    
    // I init with a layout here
    let collectionVC = CollectionViewController(collectionViewLayout: UICollectionViewFlowLayout()) 

    collectionVC.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 2)
    tabBarVC.viewControllers = [collectionVC]
    let navigationController = UINavigationController(rootViewController: tabBarVC)

    window.rootViewController = navigationController
    window.makeKeyAndVisible()
        
    self.window = window
}

Then I have my UICollectionViewController class:

class CollectionViewController: UICollectionViewController {
    
    private let searchController = UISearchController(searchResultsController: UICollectionViewController())
            
    override func viewDidLoad() {
        super.viewDidLoad()
                    
        searchController.searchResultsUpdater = self
        searchController.automaticallyShowsSearchResultsController = true
        searchController.obscuresBackgroundDuringPresentation = true
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: CollectionViewCell.reuseID)
        collectionView.keyboardDismissMode = .onDrag
        collectionView.reloadData()
        
        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action:  #selector(refreshData), for: .valueChanged)
        collectionView.refreshControl = refreshControl
    }
}

I initialize my CVC with a layout but I still have the nil layout error which doesn't make any sense:

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

CodePudding user response:

According to the docs , these are the valid initializers for a UICollectionViewController

init(collectionViewLayout: UICollectionViewLayout)

init(nibName: String?, bundle: Bundle?)

init?(coder: NSCoder)

In your scene delegate, this is done fine:

let collectionVC
    = CollectionViewController(collectionViewLayout: UICollectionViewFlowLayout()) 

However, in your CollectionViewController, you don't use any of the viable initializers when initializing UISearchController

private let searchController
    = UISearchController(searchResultsController: UICollectionViewController())

I believe it is this UICollectionViewController you are trying to instantiate with an invalid initializer that is causing your problems

  • Related