Reputation: 14470
I want to make the following UITextField appear when I compile and run the application. I have the UITextField declared and initialised as:
var myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200.00, height: 40.00));
However when I run the app, this text field does not appear. What Swift code would make the textfield appear?
Upvotes: 3
Views: 15259
Reputation: 130222
You have to actually add the text field to the view hierarchy, which is done slightly differently depending on the class that you're in. If for example, you're in a UIViewController subclass, you add it as a subview of the UIView attached to the view controller's view property.
var myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200.00, height: 40.00));
self.view.addSubview(myTextField)
Or, if you're working with a subclass of UIView, you could use:
self.addSubview(myTextField)
After you've added your text field to the view hierarchy, it's still possible that you won't be able to see it. By default, text field will have a white background color, no text, and no border around it. This can all be changed through use of the following properties.
myTextField.backgroundColor = UIColor.redColor()
myTextField.text = "some string"
myTextField.borderStyle = UITextBorderStyle.Line
Upvotes: 7
Reputation: 12868
You need to add this UITextField
as a subview of another UIView
.
Assuming you are inside a UIViewController
, you could call self.view.addSubview(myTextField)
.
Upvotes: 1