导航栏在快速显示键盘时隐藏或向上移动

问题描述 投票:0回答:1

我正在使用聊天应用程序。而且在我的应用中,当键盘出现或键盘出现在屏幕上时,我不想隐藏顶部导航栏。我正在使用以下代码实现我的目标。

 @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)
}

需要帮助谢谢

ios swift iphone keyboard uinavigationbar
1个回答
0
投票

请尝试添加以下代码来隐藏/显示键盘的隐藏/显示导航栏

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) {
 self.navigationController?.navigationBar.isHidden = false

}

@objc func keyboardWillHide(notification: Notification) {
 self.navigationController?.navigationBar.isHidden = false
}
© www.soinside.com 2019 - 2024. All rights reserved.