Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField hide placeholder text or bring cursor to front when clicked swift

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
    }
}
like image 777
richc Avatar asked Jan 07 '16 20:01

richc


1 Answers

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 = ""
like image 66
OverD Avatar answered Oct 13 '22 18:10

OverD