Wednesday, 19 October 2016

Create UIButton,UIlable and UITextfield programmatically in Swift?


UIButton:


let button = UIButton(type: .System) // let preferred over var here
        button.frame = CGRectMake(100, 100, 100, 50)
        button.backgroundColor = UIColor.greenColor()
        button.setTitle("Button", forState: UIControlState.Normal)
        button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button)





UILabel:



 var label: UILabel = UILabel()
    label.frame = CGRectMake(50, 50, 200, 21)
    label.backgroundColor = UIColor.blackColor()
    label.textColor = UIColor.whiteColor()
    label.textAlignment = NSTextAlignment.Center
    label.text = "test label"
    self.view.addSubview(label)


UITextField:


var txtField: UITextField = UITextField()
    txtField.frame = CGRectMake(50, 70, 200, 30)
    txtField.backgroundColor = UIColor.grayColor()
    self.view.addSubview(txtField)


No comments:

Post a Comment