Search code examples
iosswiftkeyboarduitextfieldxcode4.2

Moving UITextField up with keyboard - changes in swift 4.2?


I had this function to move certain textfields upwards, when the keyboards would block their view - I'm sure you all know about this:

override func viewDidLoad() {
    super.viewDidLoad()

    let center: NotificationCenter = NotificationCenter.default
    center.addObserver(self, selector: #selector(keyboardDidShow(notification:)), name: NSNotification.Name?.UIKeyboardDidShow, object: nil)
    center.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name?.UIKeyboardWillHide, object: nil)

}

But when I recently updated to swift 4.2, these stopped working, and these suggestions came up:

Replace 'UIKeyboardDidShow' with 'UIResponder.keyboardDidShowNotification' &

Replace 'UIKeyboardWillHide' with 'UIResponder.keyboardWillHideNotification

But when I pressed "fix", I get the error (x2):

Type 'NSNotification.Name' has no member 'UIResponder'

It kind of seems like a bug to me? That xCode can't accept it's own changes?? Anybody came across this and knows what to do?

Thanks!


Solution

  • Try this

    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
    notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification object: nil)
    

    More info use this link :- enter link description here

    I hope this will help you :D Thanks