Make a UIButton programmatically in Swift

后端 未结 19 1713
死守一世寂寞
死守一世寂寞 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:49

    try these..i hope it helps...

    override func viewDidLoad() {
    super.viewDidLoad()  
    
        let btn = UIButton()
        btn.frame = CGRectMake(10, 10, 50, 50)  //set frame
        btn.setTitle("btn", forState: .Normal)  //set button title
        btn.setTitleColor(UIColor.redColor(), forState: .Normal) //set button title color
        btn.backgroundColor = UIColor.greenColor() //set button background color
        btn.tag = 1 // set button tag
        btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
        self.view.addSubview(btn) //add button in view
    
    }
    

    these is buttons click event..

    func btnclicked(sender: UIButton!) 
    {
        //write the task you want to perform on buttons click event..
    }
    
    0 讨论(0)
提交回复
热议问题