3

I am using UIKeyboardWillShowNotificationto detect when a textfield is selecting by user and the keyboard is opening. In ios 8 and older versions of iOS it is working fine but today I upgrade to iOS 9 and xCode 7. the problem is that in iOS now the notification is called after selecting each textfields (while in ios 8 only called after selecting first textfield).

I searched about this and find some post like Why is UIKeyboardWillShowNotification called every time another TextField is selected?

but it is not about iOS 9 (actually every thing was right in iOS8 and by upgrading I see the problem) and I am not using inputAccessoryViews too.

Community
  • 1
  • 1
Husein Behboudi Rad
  • 5,434
  • 11
  • 57
  • 115

3 Answers3

9

Same problem here. I have some UITexfield in my view controller and when i leave a textfield for another one programmatically using becomeFirstResponder method, UIKeyboardWillShowNotification is called once again. Even if the keyboard does not hide-and-show again. However, UIKeyboardWillHideNotification is not called.

I don't know why this problem appeared in iOS9 but i can give you a workaround. What I did : I created a BOOL property in my view controller:

@property (assign, nonatomic) BOOL keyboardIsShown;

And in my Keyboard Observers methods :

- (void)keyboardWillHideNotification:(NSNotification *)notification {

    self.keyboardIsShown = NO;   
    //rest of code
}


- (void)keyboardWillShowNotification:(NSNotification *)notification {

    if(self.keyboardIsShown) {
        return;
    }

    self.keyboardIsShown = YES;   
    //some code
}

If anyone has a cleaner solution to avoid UIKeyboardWillShowNotification to be called multiple times in iOS9 when calling becomeFirstResponder on another UITextField, i would take it!

Mallox
  • 136
  • 4
  • I just see your solution. I use it too but this solution has another problem too. When you tap on the textfield repeatedly (try quickly tapping on the textfield) in this time the observers calls quickly and at all of them the keyboardIsShown flag is NO. I think it is because that they are not synchronized. Do you have any solution for this too? – Husein Behboudi Rad Oct 08 '15 at 17:20
  • did you see my comment? – Husein Behboudi Rad Oct 10 '15 at 05:25
  • I do not have the problem you describe : tapping quickly repeatedly in the textfield does not trigger UIKeyboardWillShowNotification for me. – Mallox Oct 10 '15 at 19:17
  • Thats his problem, he is using texthaschanged from the textfield instead of UIKeyboardWillShowNotification . I think that might be his problem! – Gustavo Baiocchi Costa Jan 06 '17 at 14:42
4

The UITextField's inputAssistantItem property is causing UIKeyboardWillShowNotification to get triggered twice-- once for the inputAssistantItem followed by another one for the actual keyboard.

Try setting the leadingBarButtonGroups and trailingBarButtonGroups properties of the inputAssistantItem to an empty array:

UITextInputAssistantItem* item = [textField inputAssistantItem];
item.leadingBarButtonGroups = @[];
item.trailingBarButtonGroups = @[];

Additionally, set the UITextField's autocorrectionType to UITextAutocorrectionTypeNo (you can also change this setting in the XiB file.)

textField.autocorrectionType = UITextAutocorrectionTypeNo;

What you're left with is just the software keyboard with no toolbar, and UIKeyboardWillShowNotification getting called only once.

iRon111
  • 111
  • 5
  • I think this answer goes in the right direction. But in my experience it is actually setting an `inputAccessoryView` that triggers `UIKeyboardWillShowNotification` twice. Unfortunately not setting one is not an option if you want to have it. – Wizard Nov 09 '21 at 11:47
3

I believe the UIKeyboardWillShowNotification was triggered twice because of the suggestions list above the keyboard. When I turned the Predictive off via Settings->General->Keyboard->Predictive, UIKeyboardWillShowNotification only trigger once.

Orange
  • 349
  • 3
  • 13