ios - 在单独的 UICollection 类中使用未解析的标识符 'UICollection'

标签 ios swift xcode uicollectionview

我有这门课,我将在顶部菜单 View 上工作。

这是代码:

import UIKit

class TopHomeMenuBar: UIView {

let collectionView: UICollectionView = {

    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    return cv
}()

override init(frame: CGRect) {
    super.init(frame: frame)

    addSubview(collectionView)

    backgroundColor = UIColor.systemGreen
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

但是,我不断收到错误消息:

使用未解析的标识符“UICollection”

错误是在这行代码中突出显示 UICollectionView:
    let collectionView: UICollectionView = {

最佳答案

它确实有效

现在在 XCODE 中创建了一个简单的 SinlgerView 项目
我放的 Controller (仅用于测试..通常 View 有自己的文件..)

//
//  ViewController.swift
//
//  Created by ing.conti on 11/06/2020.
//  Copyright © 2020 ing.conti. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


}


class TopHomeMenuBar: UIView {

    let collectionView: UICollectionView = {

        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        return cv
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        addSubview(collectionView)

        backgroundColor = UIColor.systemGreen
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

关于ios - 在单独的 UICollection 类中使用未解析的标识符 'UICollection',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62321332/

相关文章:

ios - 如何将选定的图像从 UIImagePicker 传递到 UIViewController

ios - 当核心数据保存操作完成时如何停止 UIActivityIndi​​cator

ios - 无法将主要的 swift 类导入测试目标?

ios - MKMapView 看起来偏黄,如何改变颜色?

iphone - 如何在不需要时停止创建对象实例

swift - 只与某些物理体交互而忽略其他物理体

ios - Swift 添加 HTML 到 UIWebView,表达式太复杂

IOS 应用程序未显示在 Firebase Analytics 中

ios - 将 AVPlayerViewController.player 设置为 nil 会破坏隐藏式字幕

ios - 升级到 iOS 5.1 和 xcode 4.3,现在应用程序无法安装