How do I create a basic UIButton programmatically?

后端 未结 30 1217
清歌不尽
清歌不尽 2020-11-22 14:41

How can I create a basic UIButton programmatically? For example in my view controller, when executing the viewDidLoad method, three UIButton<

30条回答
  •  误落风尘
    2020-11-22 15:10

    Swift3 version should be

    let myButton:UIButton = {
                let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
                myButton.setTitle("Hai Touch Me", for: .normal)
                myButton.setTitleColor(UIColor.blue, for: .normal)
                myButton.frame = CGRect(x: 20, y: 20, width: 100, height: 40)
    
                myButton.addTarget(self, action: #selector(ViewController.pressedAction(_:)), for: .touchUpInside)
                self.view.addSubview(myButton)
    
                return myButton
    
            }()
    

提交回复
热议问题