Sausage Machine Sausage Machine - 1 month ago 4x
iOS Question

Setting prompt to beginning of text in text field

I'm trying to set the the prompt to the beginning of some text in a UITextField based on this accepted answer Getting and Setting Cursor Position of UITextField and UITextView in Swift

However the prompt is getting set at the end, not the beginning. Has something changed in iOS10/Swift 3 to invalidate the code in the answer in that question?

@IBOutlet weak var mdnTextField: UITextField!

mdnTextField.text = "Some text"
mdnTextField.tintColor = UIColor.textFieldPromptLightGray()
mdnTextField.textColor = UIColor.textFieldPromptLightGray()
let promptPosition = mdnTextField.beginningOfDocument
mdnTextField.selectedTextRange = mdnTextField.textRange(from: promptPosition, to: promptPosition)


Move the promptPosition stuff to your text field's delegate's didBeginEditing.