keyboardWillShow gets called for other app's keyboards
I suggest you to check if your textField
is first responder in keyboardWillShown
method. If it is not, just ignore the notification.
func keyboardWillShow(notification: NSNotification) {
if !myTextField.isFirstResponder() {
return
}
self.keyboardIsShowing = true
if let info = notification.userInfo {
self.keyboardFrame = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
self.arrangeViewOffsetFromKeyboard()
}
}
UPDATE:
Instead of checking for the firstResponder, it is safer if you check UIApplication.shareApplication().applicationSate == .Active
iOS 9+ only:
NSNotification that comes from keyboard contains following:
UIKeyboardIsLocalUserInfoKey - The key for an NSNumber object containing a Boolean that identifies whether the keyboard belongs to the current app.
In my case i also do this (which is probably needed for OP too):
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return UIApplication.shared.applicationState == .active
}
This way keyboard won't hide when switching between applications.
Just check whether the app state is active will be fine:
- (void)handleKeyboardWillShowNotification:(NSNotification *)notifaction{
if([UIApplication sharedApplication].applicationState != UIApplicationStateActive){
return;
}
//your code below...
}