大家好,我遇到了这个错误,我不是该领域的专家,我现在正在学习,并且我不确定这是从哪里来的。该视图控制器是在情节提要上制作的,但现在我正尝试以编程方式将其转换为视图集,并弹出错误,我的问题是当我运行该应用程序并尝试打开也是建立在情节提要/上的“打开项目”时菜单栏已在代码上完成。有人以前也曾在此错误上运行过,这是什么意思?
错误:
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'-[UICollectionViewController loadView]实例化了故事板“ Main”中的标识符的视图控制器,但未获得UICollectionView。
下面是一些代码:
class Mix_Section: UICollectionViewController {
var projects : ProjectCD?
lazy var settingsLauncher : SettingsLauncher = {
let launcher = SettingsLauncher()
launcher.Section = self
return launcher
}()
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(MenuBar.self, forCellWithReuseIdentifier: "cellId")
}
private func setupMenuBar() {
// navigationController?.hidesBarsOnSwipe = true
navigationController?.navigationBar.shadowImage = UIImage()
view.addSubview(menuBar)
view.addConstraintsWithFormat(format: "H:|[v0]|", views: menuBar)
view.addConstraintsWithFormat(format: "V:|[v0(50)]|", views: menuBar)
menuBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
let brownView = UIView()
brownView.backgroundColor = .brown
view.addSubview(brownView)
view.addConstraintsWithFormat(format: "H:|[v0(50)]|", views: brownView)
}
这是菜单栏类的代码(这是我正在使用的当前集合视图):
Class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout{
var horizontalBarLeftAnchorConstraint: NSLayoutConstraint?
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .brown
cv.tintColor = .white
cv.dataSource = self
cv.delegate = self
return cv
}()
let cellId = "cellId"
//if the view controller is mixing vc or mastering vc
let imageNames = ["to-do", "task", "more-details", "extra-features"]
override init(frame: CGRect) {
super.init(frame: frame)
collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellId)
addSubview(collectionView)
addConstraintsWithFormat(format:"H:|[v0]|", views: collectionView)
addConstraintsWithFormat(format:"V:|[v0]|", views: collectionView)
let selectedIndexPath = IndexPath(item: 0, section: 0)
collectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: .left)
setupHorizontalBar()
}
func setupCollectionView() {
collectionView.backgroundColor = .white
collectionView.register(MenuCell.self, forCellWithReuseIdentifier: "cellId")
collectionView.contentInset = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
collectionView.scrollIndicatorInsets = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
}
func setupHorizontalBar() {
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.scrollDirection = .horizontal
flowLayout.minimumLineSpacing = 0
}
let horizontalBarView = UIView()
horizontalBarView.backgroundColor = .white
horizontalBarView.translatesAutoresizingMaskIntoConstraints = false
addSubview(horizontalBarView)
//old method
//horizontalBarView.frame = CGRect(x: , y: , width: , height: )
//need x,y,width,heightConstraints
horizontalBarLeftAnchorConstraint = horizontalBarView.leftAnchor.constraint(equalTo: self.leftAnchor)
horizontalBarLeftAnchorConstraint?.isActive = true
horizontalBarView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
horizontalBarView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1/4).isActive = true
horizontalBarView.heightAnchor.constraint(equalToConstant: 8).isActive = true
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MenuCell
cell.imageView.image = UIImage(named: imageNames[indexPath.item])?.withRenderingMode(.alwaysTemplate)
if indexPath.item == 4 {
cell.backgroundColor = UIColor.ultralightgray
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.item)
let x = CGFloat(indexPath.item) * frame.width / 4
horizontalBarLeftAnchorConstraint?.constant = x
UIView.animate(withDuration: 0.75, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.layoutIfNeeded()
}, completion: nil)
}
//for space in between cells
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: frame.width / 4, height: frame.height)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}