当我两次单击textview时,键盘将显示通知不起作用

时间:2018-08-02 17:48:36

标签: swift uitextview nsnotificationcenter uikeyboard

我在屏幕顶部有一个textview,在屏幕底部有一个自定义视图,如工具栏。当用户单击文本视图时,将出现键盘,并且工具栏随键盘一起上升。然后,我在工具栏上有一个关闭按钮,用于在textview上调用结束编辑功能。当我第二次单击文本视图时,工具栏没有出现,但我需要它出现。这是我附加到自定义工具栏上的代码,使它向上移动:

extension UIView{
func bindToKeyboard(){
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

@objc func keyboardWillShow(_ notification: NSNotification){

    let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
    let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
    let beginningFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    let endFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let deltaY = endFrame.origin.y - beginningFrame.origin.y



    UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {

        if UIDevice().userInterfaceIdiom == .phone && UIScreen.main.nativeBounds.height == 2436 {//check user device
            //iPhone X
             self.frame.origin.y += deltaY + 35
        }else{
             self.frame.origin.y += deltaY
        }

    }, completion: nil)
}

@objc func keyboardWillHide(_ notification: NSNotification){
    let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
    let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
    let beginningFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    let endFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let deltaY = endFrame.origin.y - beginningFrame.origin.y



    UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {

        if UIDevice().userInterfaceIdiom == .phone && UIScreen.main.nativeBounds.height == 2436 {//check user device
            //iPhone X

            self.frame.origin.y = 0
        }else{
            self.frame.origin.y = 0
        }

    }, completion: nil)
}
}

这是键盘出现时我用于textview的代码:

 override func viewDidLoad() {
    super.viewDidLoad()

    self.editView.bindToKeyboard()

   NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
     NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}



@objc func keyboardWillShow(_ notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
            textView.contentInset = contentInsets
        }
    }

@objc func keyboardWillHide(_ notification: NSNotification) {

        let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        textView.contentInset = contentInsets

}

1 个答案:

答案 0 :(得分:0)

对于键盘显示状态,请使用UIKeyboardWillShowNotification。

   NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillShow) , name: UIResponder.keyboardWillShowNotificatio, object: nil)