Make a UIButton programmatically in Swift

后端 未结 19 1777
死守一世寂寞
死守一世寂寞 2020-11-27 10:00

I am trying to build UI\'s programmatically. How do I get this action working? I am developing with Swift.

Code in viewDidLoad:

over         


        
19条回答
  •  無奈伤痛
    2020-11-27 10:44

    Swift: Ui Button create programmatically,

    var button: UIButton = UIButton(type: .Custom)
    
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0)
    
    button.addTarget(self, action: #selector(self.aMethod), forControlEvents: .TouchUpInside)
    
    button.tag=2
    
    button.setTitle("Hallo World", forState: .Normal)
    
    view.addSubview(button)
    
    
    func aMethod(sender: AnyObject) {
        print("you clicked on button \(sender.tag)")
    }
    

提交回复
热议问题