Swift 3 NSNotificationCenter Keyboardwillshow/hide
Asked Answered
C

4

11

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?

Chatoyant answered 15/6, 2016 at 2:52 Comment(10)
did u add the NotificationCenter inside viewDidLoad() or viewDidAppear() method?Ruthieruthless
Change LoginViewController.keyboardWillShow(_:) to LoginViewController.keyboardWillShow(notification:)?Cradlesong
Tried that, and xCode wants me to add the _ back inChatoyant
Are you adding the NotificationCenter observers in the LoginViewController class? Are both functions in this class too? Have you tried #selector(keyboardWillShow())?Pleuron
@Dilts the funcs are in the same class and I just tried that. The Notification observers are in the viewDidLoadChatoyant
Try func keyboardWillHide(_ notification: NSNotification) { and #selector(LoginViewController.keyboardWillHide(_:)). Notice the added underscore in the keyboardWillHide function.Pleuron
@Chatoyant check out the updated Swift Programming book. Page 1027 and 1028 are probably what you're looking for, you also might have to add a @objc(keyboardWillHideWithNotification:) to your class.Pleuron
@Dilts by combining those two comments xCode allowed me to click to edit and now its not complaining anymore...thank youChatoyant
@Dilts if you want to put it as an answer i can mark it correctChatoyant
@Chatoyant Just posted my answer! I'm glad to have helped.Pleuron
P
10

Check out the updated Swift Programming Language book. Pages 1027 and 1028 are what you're looking for. It should be something like this:

func keyboardWillHide(_ notification: NSNotification) {…

Notice the additional underscore above. Also:

#selector(LoginViewController.keyboardWillHide(_:))

You also might need to add @objc(keyboardWillHideWithNotification:) to your class.

Pleuron answered 15/6, 2016 at 10:6 Comment(0)
Y
5

On Swift 4.2, addObserver name for NSNotificationCenter changed as well:

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

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardDidHideNotification, object: nil)
Yentai answered 1/11, 2018 at 0:30 Comment(0)
M
4

Use that code that's work on swift3

You can use your ViewController (e.g, loginvc) to add notification

let loginvc : LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC

    NotificationCenter.default.addObserver(self,
        selector: #selector(loginvc.keyboardWillShow(notification:)),
        name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self,
        selector: #selector(loginvc.keyboardWillHide(notification:)),
        name: NSNotification.Name.UIKeyboardWillHide, object: nil)

Then add keyboard hide and show method

func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        print("Show") 
    }
}
func keyboardWillHide(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        print("Hide")
    }
}
Messmate answered 1/12, 2016 at 7:56 Comment(0)
T
1

NSNotificationCenter have things alter for get show keyboard:

NotificationCenter.default.addObserver(self, selector: #selector(NovaVisitaVC.abreTeclado(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(NovaVisitaVC.abreTeclado(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
Troglodyte answered 17/10, 2016 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.