gpt4 book ai didi

swift - "UICollectionView must be initialized with a non-nil layout parameter"但布局不为零

转载 作者:行者123 更新时间:2023-11-30 10:39:21 26 4
gpt4 key购买 nike

我试图设置一个 UICollectionViewController,但我总是得到“UICollectionView 必须使用非零布局参数初始化”,尽管我确实初始化了布局并将其设置在 viewDidLoad 中。

这就是我所拥有的(刚刚创建了这个 VC):

class NotificationVC: UICollectionViewController{

let cellId = "notificationCell"
let layout = UICollectionViewFlowLayout()

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavBar()
navigationItem.title = NSLocalizedString("notification.vc.title", comment: "Notification")
navigationItem.rightBarButtonItem?.tintColor = UIColor.appWhite

layout.scrollDirection = .vertical


self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView.delegate = self
collectionView.dataSource = self
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)

// Do any additional setup after loading the view.
}
}

最佳答案

当您从另一个 View Controller 创建 NotificationVC 实例时,您应该使用 UICollectionViewFlowLayout 对象初始化该类。

如果您从另一个 View Controller 推送 NotificationVC,请像这样更改它

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let notificationVC = NotificationVC(collectionViewLayout: layout)
self.navigationController?.pushViewController(notificationVC, animated: true)

关于swift - "UICollectionView must be initialized with a non-nil layout parameter"但布局不为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57208574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com