Makaveli
Makaveli

Reputation: 309

Offset UITextField - Emoji-layout

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 enter image description here

Upvotes: 1

Views: 570

Answers (3)

Makaveli
Makaveli

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

user4615157
user4615157

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. enter image description here

image 2 after emoji is selected

enter image description here

Upvotes: 1

Dejan Skledar
Dejan Skledar

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

Related Questions