I\'m new to Swift and iOS programming. I was trying to create a UITextField
programmatically using Swift but couldn\'t get it quite right. Also how do you change it
self.addSubview(myTextField)
Could work only, if you're working with the subclass of UIView
.
You have to add it as a subview of the UIView of the ViewController's hierarchy.
var txtField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 500.00, height: 30.00));
self.view.addSubview(txtField)
By default background colour is white, it just might not appear. You need to provide the style as well as text colour and background colour.
txtField.borderStyle = UITextBorderStyle.Line
txtField.text = "myString"
txtField.backgroundColor = UIColor.redColor()
For more info, here