Type 'NSNotification.Name' has no member 'keyboardDidShowNotification'

前端 未结 4 1902
傲寒
傲寒 2020-12-05 13:42

I\'m getting this error with Swift 4.2

Type \'NSNotification.Name\' has no member \'keyboardDidShowNotification\'

Here is my cod

相关标签:
4条回答
  • 2020-12-05 14:13

    I believe you use it like this now.

    NotificationCenter.default.addObserver(
        self, 
        selector: #selector(self.keyboardDidShow(notification:)), 
        name: UIResponder.keyboardDidShowNotification, object: nil) 
    
    /* You can substitute UIResponder with any of it's subclass */
    

    It is listed in UIResponder doc as a Type Property.

    0 讨论(0)
  • 2020-12-05 14:24

    Working on swift 4,2

     func bindToKeyboard(){
        NotificationCenter.default.addObserver(self, selector: #selector(UIView.keyboardWillChange(_:)), name:
            UIApplication.keyboardWillChangeFrameNotification
            , object: nil)
    
    
    }
    
    0 讨论(0)
  • 2020-12-05 14:25

    Some small details added to this answer:

    func setupViews(){
                // Keyboard notification observers
            NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    
            NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil) 
        }
    
    
        @objc func keyboardWillShow(notification: NSNotification) {
            if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                // Do something
            }
        }
    
        @objc func keyboardWillHide(notification: NSNotification) {
            // Do something
        }
    
    0 讨论(0)
  • 2020-12-05 14:37

    I have used just the UIResponder.keyboardWillHideNotification except using NSNotification.Name. . This is working in SWIFT 5

    NotificationCenter.default.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillShowNotification(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHideNotification(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    
    0 讨论(0)
提交回复
热议问题