3

I have an application where the login screen moves the username/password fields up by resizing the main view when the keyboard is shown.

The problem is that if I suspend the app (clicking the home button) while the keyboard is visible and then bring the application back, it will "reset" the view size (as per the NIB), but the keyboard will still be visible, meaning that part of the "form" becomes hidden by the keyboard.

Does anyone have any suggestions for the best way to either hide the keyboard whenever the application returns from a suspended state (I know applicationDidBecomeActive gets called on my app delegate), or make sure that the view remains resized?

Thanks in advance!

2
  • possible duplicate of How to dismiss the keyboard from the view? Commented Oct 27, 2011 at 17:45
  • Don't think it's a duplicate. Calling resignFirstResponder might do it if the control was returned to the app and something like viewWillAppear was called. If any method of my viewcontroller was called when the app comes back from suspension, resignFirstResponder could work. What I'm missing is a way to get notified, within the context of the controller, that the app is back from suspension. Commented Oct 27, 2011 at 17:51

2 Answers 2

3

Subscribe the UIApplicationDidEnterBackgroundNotification notification (via addObserver:selector:name:object: selector of NSNotificationCenter) and call [element resignFirstResponder].

I'm not sure whether it'll go down smoothly since the keyboard always animates when hiding, but at least it'll get the job done.

Sign up to request clarification or add additional context in comments.

Comments

3

I think you'll be able to achieve this using NSNotificationCenter

In your view controller, in viewWillAppear call:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWasResumed) name:UIApplicationWillEnterForegroundNotification object:nil]; 

and implement a method:

-(void) appWasResumed { [textField1 resignFirstResponder]; //If you are changing positions of items, you might want to do that here too. } 

and I think the rest should be handled automatically.

If it's not handled automatically try adding this to your AppDelegate on applicationDidBecomeActive or applicationWillEnterForeground:

[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillEnterForegroundNotification object:nil]; 

I've not used NSNotificationCenter a lot, so some of the syntax may be wrong. Check out the link I provided at the top for confirmation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.