无法识别的选择器已发送到实例,通知中心获取键盘大小

时间:2019-02-12 05:40:17

标签: swift swift4 notificationcenter

如果我尝试进入我的文本字段,则会得到一个错误,这与尝试获取移动ios设备上的键盘大小的几行代码有关。通知中心的代码行位于覆盖的ViewDidAppear内。

 NotificationCenter.default.addObserver(self, selector: Selector(("keyboardWillShow:")), name: UIResponder.keyboardDidShowNotification, object: nil)

 NotificationCenter.default.addObserver(self, selector: Selector(("keyboardWillHide:")), name: UIResponder.keyboardDidHideNotification, object: nil)

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardSize = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            self.bottomConstraint.constant = keyboardSize.height
        }
    }
}

func keyboardWillHide(notification: NSNotification) {
    self.bottomConstraint.constant = 0

}

6 个答案:

答案 0 :(得分:1)

使用类型安全的语法

#selector(keyboardWillShow)

@objc func keyboardWillShow(_ notification: Notification) { ...

但是我强烈建议使用基于现代闭包的语法

NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { [weak self] notification in
    if let userInfo = notification.userInfo,
       let keyboardSize = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            self?.bottomConstraint.constant = keyboardSize.height
    }
}
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { [weak self] _ in
    self?.bottomConstraint.constant = 0
}

答案 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) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        print("notification: Keyboard will show")
    }

}

@objc func keyboardWillHide(notification: Notification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {

    }
}

答案 2 :(得分:0)

您可以尝试以下方法:

viewDidLoad()中的此代码:

// Do any additional setup after loading the view.
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

,然后将其添加到ViewController

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
         print("Keyboard opened \(keyboardSize)")
    }
}

@objc func keyboardWillHide(notification: NSNotification) {
    print("Keyboard hide")
}

希望这会有所帮助。

答案 3 :(得分:0)

您应该注销在视图中注册的所有通知。

func registerForKeyboardNotifications()
 {
      //Adding notifies on keyboard appearing
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
 }


 func deRegisterFromKeyboardNotifications()
 {
       //Removing notifies on keyboard appearing
       NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
       NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
 }

 @objc func keyboardWasShown(_ notification: NSNotification)
 { 
     //todo
 }

 @objc func keyboardWillBeHidden(_ notification: NSNotification)
 { 
      //todo
 }

 override func viewDidLoad() {
     super.viewDidLoad()
     registerForKeyboardNotifications()
}

override func viewDidDisappear(_ animated: Bool) {
     super.viewDidDisappear(animated)
     deRegisterFromKeyboardNotifications()
}

答案 4 :(得分:0)

由于通知参数,您遇到了此错误。使用当前签名,您应该使用:


#selector(keyboardWillShow(notification:))
#selector(keyboardWillHide(notification:))

或以这种方式重写您的方法:


@objc func keyboardWillShow(_ notification: Notification) {
    // Code
}

@objc func keyboardWillHide(_ notification: Notification) {
    // Code
}

并使用以下语法:


#selector(keyboardWillShow(_:))
#selector(keyboardWillHide(_:))

已编辑:

您还可以使用简化的语法:


#selector(keyboardWillShow)
#selector(keyboardWillHide)

答案 5 :(得分:-1)

使用如下代码

NotificationCenter.default.addObserver(self,
                                       selector: #selector(keyboardWillShow(notification:)),
                                       name: UIResponder.keyboardWillShowNotification,
                                       object: nil)
NotificationCenter.default.addObserver(self,
                                       selector: #selector(keyboardWillHide(notification:)),
                                       name: UIResponder.keyboardWillHideNotification,
                                       object: nil)

@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
      // Your code
  }
}

@objc func keyboardWillHide(notification: NSNotification) {
     // Your code
}

希望这能奏效,如有疑问,请发表评论。