Friday, July 3, 2026
HomeiOS Developmentios - 'UICollectionView should be initialized with a non-nil format parameter' bug

ios – ‘UICollectionView should be initialized with a non-nil format parameter’ bug

[ad_1]

I am initializing a UICollectionViewController in SceneDelegate:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, choices connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
      
    let window = UIWindow(windowScene: windowScene)
    let tabBarVC = AppTabBarController()
    
    // I init with a format right 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’ve my UICollectionViewController class:

class CollectionViewController: UICollectionViewController {
    
    personal let searchController = UISearchController(searchResultsController: UICollectionViewController())
            
    override func viewDidLoad() {
        tremendous.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, motion:  #selector(refreshData), for: .valueChanged)
        collectionView.refreshControl = refreshControl
    }
}

I initialize my CVC with a format however I nonetheless have the nil format error which does not make any sense:

Terminating app as a result of uncaught exception
‘NSInvalidArgumentException’, cause: ‘UICollectionView should be
initialized with a non-nil format parameter’

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments