Swift При нажатии на item в tabbar происходит краш "UICollectionView must be initialized with a non-nil layout parameter"
Создан UITabBar, при нажатии на item в tabbar происходит краш "UICollectionView must be initialized with a non-nil layout parameter".
UITabBar:
class MainNavigationTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationTabBar()
}
func setupNavigationTabBar() {
let promotionsPageViewController = createNavigationController(vc: PromotionsCollectionViewController(), selected: someImage, unselected: someImage, title: "Title")
viewControllers = [ promotionsPageViewController]
guard let items = tabBar.items else {return}
for item in items {
item.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0)
}
}
}
extension UITabBarController {
func createNavigationController(vc: UIViewController, selected: UIImage, unselected: UIImage, title: String) -> UINavigationController {
let viewControllers = vc
let navController = UINavigationController(rootViewController: viewControllers)
navController.tabBarItem.image = unselected
navController.tabBarItem.selectedImage = selected
navController.tabBarItem.title = title
return navController
}
}
его items ведут на UICollectionViewController вроде такого:
class PromotionsCollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 10
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
}
extension PromotionsCollectionViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 32, left: 37.5, bottom: 0, right: 37.5)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 19.09
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "promotionCell", for: indexPath) as! PromotionsViewCell
cell.backgroundColor = .white
cell.layer.borderColor = .grey
cell.layer.cornerRadius = 14
return cell
}
}
Пожалуйста подскажите, как исправить ошибку
Ответы (1 шт):
Автор решения: lera
→ Ссылка
Добавила в UITabBarController:
let promotionsVC = PromotionsCollectionViewController(collectionViewLayout: UICollectionViewLayout())
Ошибка ушла