Swift subclass UIView

后端 未结 6 1830
时光说笑
时光说笑 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条回答
  •  -上瘾入骨i
    2020-12-07 11:25

    Here's an example of how I usually build my subclasses(UIView). I have the content as variables so they can be accessed and tweaked maybe later in some other class. I've also shown how I use auto layout and adding content.

    For example in a ViewController I have this view initialized In ViewDidLoad() since that is only called once when the view is visible. Then I use these functions I make here addContentToView() and then activateConstraints() to build the content and set constraints. If I later in a ViewController want the color of let's say a button to be red, I just do that in that specific function in that ViewController. Something like: func tweaksome(){ self.customView.someButton.color = UIColor.red}

    class SomeView: UIView {
    
    
    var leading: NSLayoutConstraint!
    var trailing: NSLayoutConstraint!
    var bottom: NSLayoutConstraint!
    var height: NSLayoutConstraint!
    
    
    var someButton: UIButton = {
        var btn: UIButton = UIButton(type: UIButtonType.system)
        btn.setImage(UIImage(named: "someImage"), for: .normal)
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    }()
    
    var btnLeading: NSLayoutConstraint!
    var btnBottom: NSLayoutConstraint!
    var btnTop: NSLayoutConstraint!
    var btnWidth: NSLayoutConstraint!
    
    var textfield: UITextField = {
        var tf: UITextField = UITextField()
        tf.adjustsFontSizeToFitWidth = true
        tf.placeholder = "Cool placeholder"
        tf.translatesAutoresizingMaskIntoConstraints = false
        tf.backgroundColor = UIColor.white
        tf.textColor = UIColor.black
        return tf
    }()
    var txtfieldLeading: NSLayoutConstraint!
    var txtfieldTrailing: NSLayoutConstraint!
    var txtfieldCenterY: NSLayoutConstraint!
    
    override init(frame: CGRect){
        super.init(frame: frame)
        self.translatesAutoresizingMaskIntoConstraints = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //fatalError("init(coder:) has not been implemented")
    }
    
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    
    }
    */
    func activateConstraints(){
        NSLayoutConstraint.activate([self.btnLeading, self.btnBottom, self.btnTop, self.btnWidth])
        NSLayoutConstraint.activate([self.txtfieldCenterY, self.txtfieldLeading, self.txtfieldTrailing])
    }
    
    func addContentToView(){
        //setting the sizes
        self.addSubview(self.userLocationBtn)
    
        self.btnLeading = NSLayoutConstraint(
            item: someButton,
            attribute: .leading,
            relatedBy: .equal,
            toItem: self,
            attribute: .leading,
            multiplier: 1.0,
            constant: 5.0)
        self.btnBottom = NSLayoutConstraint(
            item: someButton,
            attribute: .bottom,
            relatedBy: .equal,
            toItem: self,
            attribute: .bottom,
            multiplier: 1.0,
            constant: 0.0)
        self.btnTop = NSLayoutConstraint(
            item: someButton,
            attribute: .top,
            relatedBy: .equal,
            toItem: self,
            attribute: .top,
            multiplier: 1.0,
            constant: 0.0)
        self.btnWidth = NSLayoutConstraint(
            item: someButton,
            attribute: .width,
            relatedBy: .equal,
            toItem: self,
            attribute: .height,
            multiplier: 1.0,
            constant: 0.0)        
    
    
        self.addSubview(self.textfield)
        self.txtfieldLeading = NSLayoutConstraint(
            item: self.textfield,
            attribute: .leading,
            relatedBy: .equal,
            toItem: someButton,
            attribute: .trailing,
            multiplier: 1.0,
            constant: 5)
        self.txtfieldTrailing = NSLayoutConstraint(
            item: self.textfield,
            attribute: .trailing,
            relatedBy: .equal,
            toItem: self.doneButton,
            attribute: .leading,
            multiplier: 1.0,
            constant: -5)
        self.txtfieldCenterY = NSLayoutConstraint(
            item: self.textfield,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0.0)
    }
    }
    

提交回复
热议问题