The fix-it’s should update the code to Swift 4.2.
Apple changed these properties (again) in Swift 4.2 and the iOS 12 SDK.
I’ll be posting some new videos for the changes in the near future.
Both of these enum values have been renamed.
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
is now
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NSNotification.Name.UIKeyboardWillShow
became: UIResponder.keyboardWillShowNotification
There are a few more that have changed using a similar format in the viewDidLoad()
method.
Your viewDidLoad() and deinit should look something like:
override func viewDidLoad() {
super.viewDidLoad()
billTextField.delegate = self
print("Hello")
print("Hi Paul! \(18)")
// Listen for keyboard events
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
deinit {
// Stop listening for keyboard hide/show events
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
Then in the keyboardWillChange()
method, you’ll need to change the keys.
guard let keyboardRect = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
is now
guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
Where UIKeyboardFrameEndUserInfoKey
morphed into UIResponder.keyboardFrameEndUserInfoKey
The method should look something like:
@objc func keyboardWillChange(notification: Notification) {
guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
return
}
if notification.name == UIResponder.keyboardWillShowNotification ||
notification.name == UIResponder.keyboardWillChangeFrameNotification {
view.frame.origin.y = -keyboardRect.height
} else {
view.frame.origin.y = 0
}
}