Reputation: 309
I am trying to offset UITextField whenever the keyboard is active, it works well, until I tried the Emoji-layout. Is there a way to detect the type of Keyboard-input, so I can find out the height-difference?
Thanks
Upvotes: 1
Views: 570
Reputation: 309
Thanks guys for your help, i have found the answer: I was using this line :
if let keyboardSize = (notifcation.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size{
I changed to the key-value of notifcation.userInfo to UIKeyboardFrameEndUserInfoKey, and that fixed the issue.
Upvotes: 0
Reputation:
You can use the keyboard notifications
func getKeyboardHeight() {
let defaultCenter = NSNotificationCenter.defaultCenter()
defaultCenter.addObserver(self, selector: "keyboardWillChangeFrame:", name: UIKeyboardWillChangeFrameNotification, object: nil)
}
func keyboardWillChangeFrame(notification : NSNotification){
let keyboardFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let keyboardheight = keyboardFrame.height
}
And from the obtained height you can adjust the textfield's frame.
see the images.
image 1 before emoji is selected.
image 2 after emoji is selected
Upvotes: 1
Reputation: 11435
Instead of using the UIKeyboardDidShowNotification/UIKeyboardDidHideNotification
observers, use the UIKeyboardWillChangeFrameNotification
observer, that is fired of each event: Keyboard hiding, keyboard showing and keyboard changing frame.
Like this:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardAction), name: UIKeyboardWillChangeFrameNotification, object: nil)
Upvotes: 1