Here is what you want, Selector with string type and Notification parameter argument
Swift 4
NotificationCenter.default.addObserver(self, selector: Selector(("showKeyboard:")), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
var keyboardHeight = 0.0
//-------------------
@objc func showKeyboard(_ sender: Notification) {
keyboardWillShow(sender: sender as NSNotification, adjustHeight: 150)
print("sender - \(sender)")
}
//-------------------
func keyboardWillShow(sender: NSNotification, adjustHeight: CGFloat) {
if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
keyboardHeight = Double(keyboardSize.height)
// do your calculations
}
}
Swift 3
NotificationCenter.default.addObserver(view, selector: Selector(("keyboardWillShow:")), name: .UIKeyboardWillShow, object: anyView.view.window)
func keyboardWillShow(_ notification: Notification) {
keyboardWillShow(sender: sender as NSNotification, adjustHeight: 150)
print("sender - \(sender)")
}
Here are normal selector, according to language support
Swift 4
NotificationCenter.default.addObserver(self, selector: #selector(self.showKeyboard(sender:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
var keyboardHeight = 0.0
//-------------------
@objc func showKeyboard(sender: NSNotification) {
keyboardWillShow(sender: sender, adjustHeight: 150)
}
//-------------------
func keyboardWillShow(sender: NSNotification, adjustHeight: CGFloat) {
if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
keyboardHeight = Double(keyboardSize.height)
// your operations here
}
}
Swift 3
NotificationCenter.default.addObserver(self, selector: #selector(self.showKeyboard(sender:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
var keyboardHeight = 0.0
//-------------------
func showKeyboard(sender: NSNotification) {
keyboardWillShow(sender: sender, adjustHeight: 150)
}
//-------------------
func keyboardWillShow(sender: NSNotification, adjustHeight: CGFloat) {
if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
keyboardHeight = Double(keyboardSize.height)
// your operations here
}
}