Skip to content

Instantly share code, notes, and snippets.

@lailo
Last active November 16, 2017 16:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lailo/8765a4def31aa2ae197a to your computer and use it in GitHub Desktop.
Save lailo/8765a4def31aa2ae197a to your computer and use it in GitHub Desktop.
Keyboard height for iOS in Swift
// Add this to viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "keyboardShown:", name: UIKeyboardDidShowNotification, object: nil)
// This function handles notification
func keyboardShown(notification: NSNotification) {
let info = notification.userInfo!
let value: AnyObject = info[UIKeyboardFrameEndUserInfoKey]!
let rawFrame = value.CGRectValue()
let keyboardFrame = view.convertRect(rawFrame, fromView: nil)
println("keyboardFrame: \(keyboardFrame)")
}
@pawargb
Copy link

pawargb commented Nov 16, 2017

updated below and it worked for me :

NotificationCenter.default.addObserver(self, selector: #selector(keyboardShown), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
and added @objc keyword before function

However I didn't understand how this code works, if you can explain line by line would be great.
Thank you in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment