Marcel Marcel - 1 year ago 121
Swift Question

UITextField hide cursor after return is pressed

I want to have an editable title for my Navigation Bar. I found this code

let navigationTitlelabel = UITextField(frame: CGRectMake(0, 0, 200, 21)) = CGPointMake(160, 284)
navigationTitlelabel.textAlignment = NSTextAlignment.Center
navigationTitlelabel.textColor = UIColor.whiteColor()
navigationTitlelabel.text = defaultListName
self.navigationController!.navigationBar.topItem!.titleView = navigationTitlelabel
navigationTitlelabel.delegate = self

I have it in
. This is so I can change the title of the view controller. Then I implement these for the delegate:

func textFieldShouldReturn(textField: UITextField) -> Bool {
return true

func textFieldShouldEndEditing(textField: UITextField) -> Bool {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let moc = appDelegate.managedObjectContext

if textField.text != ""{
//save to Core Data
return true
return false

However when I press return, the cursor still blinks. The data IS saved, and if I reload the VC the title is the new one, but I can't get out of the "editing the title mode" even if I tap another button.

How can I solve this issue?

I'm testing this on the simulator, Swift 2.2

Answer Source

Modify textFieldShouldReturn with below code

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download