У меня есть UITextView в моем ViewController. Когда пользователь нажимает на этом textView, появляется клавиатура.Это не проблема.Моя проблема: Как скрыть клавиатуру, когда пользователь нажимает кнопку возврата на клавиатуре?Я пробовал некоторые функции, но, видимо, они работали только с UITextFields.
import UIKit
class NewNoteVC: UIViewController, UITextViewDelegate {
@IBOutlet weak var newText: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
newText.text = "Start typing"
newText.textColor = UIColor.lightGray
newText.tintColor = UIColor(red: 251/255, green: 140/255, blue: 139/255, alpha: 1)
}
/// setting placeholdere
func textViewDidBeginEditing(_ textView: UITextView) {
if newText.textColor == UIColor.lightGray {
newText.text = nil
newText.textColor = UIColor.black
}
}
func textViewDidEndEditing(_ textView: UITextView) {
if newText.text.isEmpty {
newText.text = "Placeholder"
newText.textColor = UIColor.lightGray
}
}
/// limit characters
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let currentText = newText.text ?? ""
guard let stringRange = Range(range, in: currentText) else { return false }
let changedText = currentText.replacingCharacters(in: stringRange, with: text)
return changedText.count <= 1000
}
/// hide keyboard when user touch outside screen
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
/// hide keyboard when user press return button
@IBAction func backToMainPage(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}