Last active
November 16, 2017 16:07
-
-
Save lailo/8765a4def31aa2ae197a to your computer and use it in GitHub Desktop.
Keyboard height for iOS in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!