I have a piece of code that worked in Swift 2 and I tried using Xcode to update the code to the newest version and I fixed everything except two issues.
I have this code :
let loginvc: LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
That pairs along with this:
func keyboardWillShow(notification: NSNotification) {
constraint.constant = -100
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
func keyboardWillHide(notification: NSNotification) {
constraint.constant = 25
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
On the first part I now get an error saying
Type 'LoginViewController' has no member 'keyboardWillShow/Hide'
I don't understand why it is not seeing the method underneath.
Does anybody know a solution to this issue?
LoginViewController.keyboardWillShow(_:)
toLoginViewController.keyboardWillShow(notification:)
? – CradlesongLoginViewController
class? Are both functions in this class too? Have you tried#selector(keyboardWillShow())
? – Pleuronfunc keyboardWillHide(_ notification: NSNotification) {
and#selector(LoginViewController.keyboardWillHide(_:))
. Notice the added underscore in the keyboardWillHide function. – Pleuron@objc(keyboardWillHideWithNotification:)
to your class. – Pleuron