2015-02-02 17 views
7

Mam problem z odrzuceniem klawiatury widoku tekstowego w trybie szybkim.Swift Dismiss UITextView Keyboard

byłem w stanie oddalić się pole tekstowe z następujących

@IBAction func DismissKeyboard(sender: AnyObject) 
    { 
     self.resignFirstResponder() 
    } 

Ale nie jestem pewien, jak ja go o to z widoku tekstu

Odpowiedz

22

Musisz ustawić textview.delegate do siebie i korzystać z funkcji shouldChangeTextInRange do dymisji po naciśnięciu powrót.

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { 
    if text == "\n" // Recognizes enter key in keyboard 
    { 
     textView.resignFirstResponder() 
     return false 
    } 
    return true 
} 
+1

Bardzo dziękuję za pomoc w tej sprawie i mam nadzieję, że to pomoże również innym: D – Azabella

0

wystarczy dodać UITextView jako IBOutlet i użyj Ta sama funkcja:

@IBAction func DismissKeyboard(sender: AnyObject) { 
    yourtextView.resignFirstResponder() 
} 
+0

jak mogę zmusić go do ustąpienia na przycisk Gotowe? – Azabella

2

Swift 3

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { 
    if text == "\n" { 
     textView.resignFirstResponder() 
     return false 
    } 
    return true 
}