我的ViewController中有一个UITextView,当用户在该textView上按下时,将显示键盘。这不是问题。我的问题是:当用户按下键盘上的返回按钮时,如何隐藏键盘?我尝试了一些功能,但显然它们仅与UITextFields一起使用。
from itertools import groupby
from string import ascii_lowercase as letters
vowels = "aeiou"
is_vowel = vowels.__contains__
partitions = [list(g) for k, g in groupby(letters, is_vowel)]
mapping = {}
for curr_letters, next_letters in zip(partitions, partitions[1:]):
for letter in curr_letters:
mapping[letter] = next_letters[0]
table = str.maketrans(mapping)
"orange".translate(table)
# 'puboif'
答案 0 :(得分:1)
无论出于什么原因而在退回UITextView
时关闭键盘,最简单的解决方案是:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
}
{...}
}