如何为在 swift 中以编程方式创建的视图定义 class 和模块

How to define a class and module for a view created programmatically in swift

假设我创建了这样一个视图:

let myView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.view.addSubview(myView)

如何像在 IB 中那样为该视图提供自定义 classmodule

我不是在搜索错误的东西,就是没有人问过这个问题。我也没有尝试任何东西,因为我不知道从哪里开始。

更新:

这就是我在 IB 中添加 classmodule 的意思:

简单地实例化它而不是UIView

假设这是您的自定义视图:

class MyCustomView: UIView {
    //...
}

实例化方法如下:

let myView = MyCustomView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.view.addSubview(myView)

该模块是您 class MyCustomView: UIView... 的源文件所属的模块。在开发 iOS 应用程序(不是框架或其他目标类型)时,这就是您的应用程序。选择源文件时可以在Xcodes Inspector中选择"Target Membership":