UIButton
let topBgView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.width))
view.addSubview(topBgView)
topBgView.backgroundColor = UIColor.yellow
let customBtn = UIButton.init(frame: CGRect.init(x: 66, y: 66, width: 64, height: 44))
customBtn.setTitle("btnTitle", for: .normal)
customBtn.setTitleColor(UIColor.red, for: .normal)
customBtn.addTarget(self, action: #selector(clickBtn), for: .touchUpInside)
topBgView.addSubview(customBtn)
@objc func clickBtn( ) -> Void {
print("我是按钮")
}
UILable
let customLable = UILabel.init(frame: CGRect.init(x: customBtn.frame.midX , y: 66 + 44, width: 66, height: 44))
topBgView.addSubview(customLable)
customLable.text = "lable"
customLable.textColor = UIColor.green
UITextField
let customTextField = UITextField.init(frame: CGRect.init(x: customLable.frame.minX, y: customLable.frame.midY, width: 44, height: 44))
topBgView.addSubview(customTextField)
customTextField.placeholder = "haha"
customTextField.textColor = UIColor.red
customTextField.borderStyle = .roundedRect
差不多就这套路。语法可以参考:www.runoob.com/swift/swift…
oc里面的懒加载》swift
lazy var selectBtn: UIButton = {
let button = UIButton.init(type: .custom)
button.setTitle("登录", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
button.backgroundColor = UIColor.red
button.layer.cornerRadius = 5
button.layer.masksToBounds = true
return button
}()