Question

I have one ViewController with custom view class. I have handled keyboard notification by adding into viewWillAppear method and remove notification in viewDidDisappear.

Notification adding into viewWillAppear:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector (keyboardWillShow:)
                                             name: UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector (keyboardWillHide)
                                             name: UIKeyboardWillHideNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector (keyboardDidHide)
                                             name: UIKeyboardDidHideNotification object:nil];

Remove Notification into viewDidDisappear:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object: nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object: nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object: nil];

and call view's method from viewController's notification method.

Issue:

  • After tapping on UITextField, keyboard appear and properly animation done
  • But after keyboard appear, if user press back button from navigation-bar then keyboard will not remove after ViewController disappear from screen.
  • Now user not able to close keyboard

If I remove notification code then worked perfectly.

Before iOS 7.0, its works perfectly with notification code. But in iOS 7.0, its not work.

I have print NSLog in each notification method, in IOS 7.0 keyboardWillShow method executes at last and again keyboard appear. But in iOS 6.0, keyboardWillShow not execute at last.

I have also implements UITextFieldDelegate methods.

Thanks in advance

Was it helpful?

Solution

Below you can see difference in keyboard life cycle for retained (will not dealloc after pop) pushed viewController with textField/textView which becomes the firstResponder.

Let we add keyboard observers as follows

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}

- (void) viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}    

iOS6 keyboard life cycle for iPhone 5

  • viewWillAppear:
  • [YourTextField becomeFirstResponder]
  • keyboardWillShow: keyboardFrame = {0, 306, 320, 262}

  • [back button tapped]

  • keyboardWillHide: keyboardFrame = {320, 306, 320, 262}
  • viewDidDisappear:

  • [push existing view controller]

  • viewWillAppear:
  • keyboardWillShow: keyboardFrame = {0, 306, 320, 262}

iOS7 keyboard life cycle for iPhone 5

  • viewWillAppear:
  • [YourTextField becomeFirstResponder]
  • keyboardWillShow: keyboardFrame = {0, 308, 320, 260}

  • [back button tapped]

  • keyboardWillHide: keyboardFrame = {0, 568, 320, 260}
  • keyboardDidHide:
  • keyboardWillShow: keyboardFrame = {0, 308, 320, 260}
  • keyboardWillHide: keyboardFrame = {0, 320, 320, 260}
  • viewDidDisappear:

  • [push existing view controller]

  • viewWillAppear:
  • keyboardWillShow: keyboardFrame = {0, 308, 320, 260}

The difference is that iOS7 formally tries to hide keyboard pop but that shows keyboard again.

Thus if you want to hide keyboard in both cases you need to resign textField explicitly in viewWillDisappear. Note that textField.isFirstResponder equals NO in viewWillDisappear but

[textField resignFirstResponder]; 

solves the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top