Swift subclass UIView

后端 未结 6 1859
时光说笑
时光说笑 2020-12-07 10:49

I want to subclass UIView and show a login like view. I\'ve created this in Objective-C, but now I want to port it to Swift. I do not use storyboards, so I crea

6条回答
  •  青春惊慌失措
    2020-12-07 11:30

    I usually do something like this, its a bit verbose.

    class MyView: UIView {
        override init(frame: CGRect) {
            super.init(frame: frame)
            addBehavior()
        }
    
        convenience init() {
            self.init(frame: CGRect.zero)
        }
    
        required init(coder aDecoder: NSCoder) {
            fatalError("This class does not support NSCoding")
        }
    
        func addBehavior() {
            print("Add all the behavior here")
        }
    }
    
    
    
    let u = MyView(frame: CGRect.zero)
    let v = MyView()
    

    (Edit: I've edited my answer so that the relation between the initializers is more clear)

提交回复
热议问题