我遇到了以下问题。 我有一个自定义类,UIView 的子类 在其中,我尝试添加一个新的 UIView,但它不允许我这样做。
class myCustomViewClass: UIView {
let smallerView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
self.view.addSubview(smallerView) // Error with this !
}
我尝试过不同的东西。 我知道如果我改为从 UIViewController 继承,我可以让它工作,但我真的不想那样做。
感谢任何帮助。谢谢!
最佳答案
class myCustomViewClass: UIView {
let smallerView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
override func drawRect(rect: CGRect) {
self.addSubview(smallerView)
}
}
关于class - 将 UIView 作为 SubView 添加到自定义类,UIView 的子类,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28160228/