Is there an up to date swift solution for making a UITextField placeholder disappear, or the cursor to be brought to the front as soon as a user clicks in the field, rather than when they start typing. I tried some of the solutions from older posts, and I tried the code below, but it doesn't seem to have the desired effect. Any help much appreciated.
class LogInViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var userTextField: UITextField!
// the placeholder text is set in IB, with clear Button set to Appears while editing, and clear when editing begins button checked.
func textFieldDidBeginEditing(textField: UITextField) {
userTextField.placeholder = nil
passwordTextField.placeholder = nil
}
}
This worked for me:
adding userTextField.delegate = self to view did load. // thanks I missed that
func textFieldDidBeginEditing(textField: UITextField) {
if userTextField.isFirstResponder() == true {
userTextField.placeholder = nil
}
}
First thing make sure you set the delegate of the text field either from storyboard or set it in the viewDidLoad()
And change your code to this
userTextField.placeholder = ""
passwordTextField.placeholder = ""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With