0

If I try to tap into my textfields I get an error, related to these few lines of code that try to get the size of the keyboard on a mobile ios device. The Notification Center lines of code are inside the overriding 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

}
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Niccolò Diana
  • 119
  • 2
  • 13

6 Answers6

2

Use the type safe syntax

#selector(keyboardWillShow)

and

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

However I highly recommend to use the modern closure based syntax

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
}
vadian
  • 274,689
  • 30
  • 353
  • 361
0

Try Following code :

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 {

    }
}
Jarvis The Avenger
  • 2,750
  • 1
  • 19
  • 37
0

You can try this:

This code in 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)

and then add this in 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")
}

Hope this will help.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
0

You should deregister any notification you register in a view.

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()
}
Rubaiyat Jahan Mumu
  • 3,887
  • 1
  • 33
  • 35
0

You've got this error because of notification parameter. With current signature you should use:


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

Or rewrite your methods in that way:


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

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

And use the next syntax:


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

Edited:

You can also use simplified syntax:


#selector(keyboardWillShow)
#selector(keyboardWillHide)

J. Doe
  • 114
  • 5
-1

Use like below code

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
}

Hope this works, If any doubt plz comment.

vivekDas
  • 1,248
  • 8
  • 12