Use UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey
So changing your code to the following will fix your issue:
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
print(keyboardSize)
}
}