keyboardWillShow called twice
Asked Answered
B

3

10

I have a view with keyboard notifications such as keyboardWillShow and keyboardWillHide

All the codes handles with the notification I use is taken from Apple's sample code "KeyboardAccessory"

When I first enter this view, everything works fine.

But when I return to this view from its subviews, every time I tap a button that says:

[myTextField becomeFirstResponder];

the keyboardWillShow and keyboardWillHide methods will be called twice every time.

It's really confusing,

Could anyone helps me with this?

really appreciate!

Beverleebeverley answered 10/11, 2011 at 16:4 Comment(0)
D
14

You might want to post your code.

If your methods are being called twice, most likely, you are registering for the keyboard notifications multiple times and not removing the observer when you think you are.

Add your observer in viewWillAppear: and remove it in viewWillDisappear: and see what happens.

Deneb answered 10/11, 2011 at 20:44 Comment(3)
Thanks! I suddenly get known where the problem is!Beverleebeverley
But what about telling us the solution for your problem? Sounds like its something else than removing the observer from your viewcontrollerCognac
In my case, I did indeed have the observer added twice. Thanks~Vitalis
T
9

It is called twice on iOS 8 when a custom keyboard, like SwiftKey is being shown.

On the first call, it probably prepares to show, so CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; will have a keybaord height of 0.

The second call will have the correct keyboard height, so if you need to adjust content insets, you can check that the height of keyboard returned is > 0 before calculating your insets.

Also, it will be called whenever user changes keyboard.

Tellurize answered 27/12, 2014 at 8:31 Comment(1)
I am getting this with exactly the same notification (object address is equal) and the height is equal, too... :-( Also, I am only adding the observer once in viewDidLoadCaiman
P
8

For me, changing inputAccessoryView would fire another call of keyboardWillShow because the height of an input accessory view affects the overall keyboard height.

Pharos answered 10/6, 2016 at 15:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.