I've UITextView, next to it and separate from text view is a send button. After user types text in text view and taps send button, keyboard hides. I don't want to hide keyboard after user taps on send button. How do I achieve this?
Updating question to provide more details. 1) Send Button is a separate UIButton. 2) I don't have any resignFirstResponder being called exclusively in the code. 3) After send button is clicked I've following code which shows/hides keyboard.
override func viewDidLoad() {
super.viewDidLoad()
// Setup keyboard event
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
}
// Setup keyboard event
@objc func keyboardWillShow(notification:NSNotification){
print("keyboardWillShow")
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.view.convert(keyboardFrame, from: nil)
var contentInset:UIEdgeInsets = self.scrollView.contentInset
contentInset.bottom = keyboardFrame.size.height
scrollView.contentInset = contentInset
}
@objc func keyboardWillHide(notification:NSNotification){
print("keyboardWillHide")
let contentInset:UIEdgeInsets = UIEdgeInsets.zero
scrollView.contentInset = contentInset
}
Send button clicked code
@IBAction func sendButtonClicked(_ sender: UIButton) {
// Disable textview and send button while message is being sent
messageTextView.isEditable = false
sendButton.isEnabled = false
fetchSendMessage( ) { (result, error) in
if error != nil {
ShowDialog.showDialog(title: nil, message: (error?.localizedDescription)!, viewController: self)
} else {
if (result?.success)! {
self.arry = (result?.data)!
self.fetchMessages(page: 1, completed: {
self.insertNewMessage(sentMessage)
})
// Clear the message & Enable textview and send button
self.messageTextView.isEditable = true
self.messageTextView.text = "Type Message"
self.messageTextView.textColor = UIColor.greyColour
self.sendButton.isEnabled = true
self.messageTextView.endEditing(true)
} else {
ShowDialog.showDialog(title: "Message not sent", message: (result?.errors![0].message)!, viewController: self)
// Retain typed message
// Enable textview and send button
self.messageTextView.isEditable = true
self.sendButton.isEnabled = true
}
}
}
}
textView.returnKeyType = .send
, the Send key still inserts a newline into the text view and does not dismiss the keyboard. Can you detail your view setup? – Code Different