How to use a button to add a UITextField in Swift

All I want to do is use the button to add another UITextField That looks exactly the same right below the first one. I would like a new block to appear below every time the button is pressed. Thanks Picture is below.

Is the text field always going to be there and you just want the user to tap on the button first to see it? because if so, you could simply make it invisible until the point when the user taps the button, make it visible then.

No I basically want the UITextField to be added below the other one that is already there. I would like to append it.

Ok Ive never done that, but I guess you could instantiate a new UITextField in code (well this much I know) from the press of a button, but then call to redraw the viewcontroller’s view in order to display it.

You would also have to do some math to make them fit and align properly, otherwise they may be added but you won’t physically see them onscreen because they will be squished or displaced by other elements already on the screen.

To redraw the views you need to go deep into the land of UIViews.

look into:
self.setNeedsDisplay()

@IBAction func buttonPressed (sender: UIButton) {
let myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200.00, height: 40.00));
self.view.addSubview(myTextField)
myTextField.text = “Hello”
print(myTextField)
}

That Worked thank you so much! I was wondering if there is a way to position it lower every time the button is clicked because right now if the button is clicked the new textfield just is written over the older one.

You would need to create some sort of state function to generate new text fields with new names and new positions each time.