     }
 
     override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
+        shouldApplyCompletion = completionActive
         if !completionActive {
             super.touchesBegan(touches, withEvent: event)
         }

    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if !completionActive {
            super.touchesEnded(touches, withEvent: event)
        } else {
            applyCompletion()

            // Set the current position to the end of the text.
            selectedTextRange = textRangeFromPosition(endOfDocument, toPosition: endOfDocument)
        }
    }
}

    private var completionActive = false
    private var canAutocomplete = true

    // This variable is a solution to get the right behavior for refocusing
    // the AutocompleteTextField. The initial transition into Overlay Mode 
    // doesn't involve the user interacting with AutocompleteTextField.
    // Thus, we update shouldApplyCompletion in touchesBegin() to reflect whether
    // the highlight is active and then the text field is updated accordingly
    // in touchesEnd() (eg. applyCompletion() is called or not)
    private var shouldApplyCompletion = false
    private var enteredText = ""
    private var previousSuggestion = ""
    private var notifyTextChanged: (() -> ())? = nil
