How do I create a basic UIButton programmatically?

后端 未结 30 920
清歌不尽
清歌不尽 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:28

    For Swift 3 (even shorter code)

    let button = UIButton(type: UIButtonType.custom)
    button.frame = CGRect(x: 0, y: 0, width: 200.0, height: 40.0)
    button.addTarget(nil, action: #selector(tapButton(_:)), for: UIControlEvents.touchUpInside)
    button.tintColor = UIColor.white
    button.backgroundColor = UIColor.red
    button.setBackgroundImage(UIImage(named: "ImageName"), for: UIControlState.normal)
    button.setTitle("MyTitle", for: UIControlState.normal)
    button.isEnabled = true
    
    func tapButton(sender: UIButton) {
    
    }
    

提交回复
热议问题