我正在使用聊天应用程序。而且在我的应用中,当键盘出现或键盘出现在屏幕上时,我不想隐藏顶部导航栏。我正在使用以下代码实现我的目标。
@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
}
}
呈现键盘之前
显示键盘后
关闭键盘后
我认为您在keyboardSize
中的keyboardWillHide
不正确,请尝试使用UIKeyboardFrameEndUserInfokey
而不是UIKeyboardFrameBeginUserInfoKey
请尝试添加以下代码来隐藏/显示键盘的隐藏/显示导航栏
此外,您还可以根据键盘大小以相同的方法管理滚动。
更新后的答案::您可以将约束设置为包含文本字段的视图底部,并在键盘show \ hide上对其进行更新。这个对我有用。
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillShow(notification: Notification) {
guard let userInfo = (sender as Notification).userInfo, let value = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let newHeight: CGFloat
if #available(iOS 11.0, *) {
newHeight = value.cgRectValue.height - view.safeAreaInsets.bottom
} else {
newHeight = value.cgRectValue.height
}
self.chatViewBottomConstraints.constant = newHeight
DispatchQueue.main.async {
self.tableView.scrollToBottom()
}
}
@objc func keyboardWillHide(notification: Notification) {
DispatchQueue.main.async {
self.chatViewBottomConstraints.constant = 0.0
self.tableView.scrollToBottom()
}
}