Majid Majid - 1 month ago 47
Swift Question

swift 3, unrecognized selector sent to instance ( KeyboardWillShow)

I search a lot about it on stack overflow but according to their solution my program is same as mention but still not working.

func subscribeToKeyboardNotifications(){

NotificationCenter.default.addObserver(self, selector:Selector(("keyboardWillShow:")), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
}


func keyboardWillShow(notification:NSNotification){

view.frame.origin.y -= getKeyboardHeight(notification: notification)
}

Answer

Your argument for selector should be #selector(keyboardWillShow), like so:

func subscribeToKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
}

func keyboardWillShow(notification:NSNotification) {
    view.frame.origin.y -= getKeyboardHeight(notification: notification)
}