Moving UITextField up with keyboard - changes in swift 4.2?

自古美人都是妖i 提交于 2019-12-11 17:49:47

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/53082764/moving-uitextfield-up-with-keyboard-changes-in-swift-4-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!