Я работаю над приложением чата. И в моем приложении я не хочу скрывать свою верхнюю панель навигации, когда клавиатура появляется или когда клавиатура отображается на экране. Я использую следующий код для достижения своей цели.
@objc func keyboardWillShow(notification: NSNotification) {
var newYpos = CGFloat()
let rate = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
self.customView.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
newYpos = keyboardSize.height
// print(newYpos)
self.tap.isEnabled = true
}
if self.yPosAfterFirstNotif == 0.0{
self.yPosAfterFirstNotif = newYpos
UIView.animate(withDuration: rate.doubleValue, animations: {
self.customView.bottomC.constant = (-self.yPosAfterFirstNotif)
})
}
self.customView.tableView.contentInset = UIEdgeInsetsMake(0, 0, newYpos, 0)
//scrollToBottom()
self.customView.tableView.scrollToBottomm()
self.customView.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
}
И вот как я скрываю свою клавиатуру
@objc func keyboardWillHide(notification: NSNotification) {
var newYpos = CGFloat()
let rate = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
{
newYpos = keyboardSize.height
self.yPosAfterFirstNotif = newYpos
self.customView.tableView.contentInset = UIEdgeInsetsMake(0, 0, newYpos, 0)
}
UIView.animate(withDuration: rate.doubleValue, animations: {
self.customView.bottomC.constant = (+self.yPosAfterFirstNotif)
})
// self.constraintCommentViewBottom.constant = 0
self.yPosAfterFirstNotif = 0.0
self.customView.tableView.contentInset = UIEdgeInsetsMake(0, 0, newYpos, 0)
self.tap.isEnabled = false
if self.customView.ChatTextField.text.characters.count == 0
{
self.customView.sendButton.isHidden = true
self.customView.recordButton.isHidden = false
self.customView.cameraButton.isHidden = false
self.customView.textFieldTrailling.constant = 44
}
}
перед представлением клавиатуры

после представления клавиатуры

после увольнения клавиатуры
Нужна помощь Спасибо