Question about passing a NSNotification for scrolling a view when keyboard shows - iphone

I have a view with five UITextFields spaced from top to bottom. When you click in a text field the keyboard pops up and covers the bottom two text fields. I have tweaked the code to the point where if you click in one of the bottom two text fields the view will scroll up so that text field is visible.
The problem I am having is when you start in the first text field and tab through the text fields the view does not scroll when you get to the last two text field. I have determined this is because when tabbing between views my method keyboardWillShow never gets called again after we have clicked in the first text Field.
I have tried using the textFieldDidBeginEditing method to determine which text field now has focus and then I wanted to call my keyboardWillShow method again. But that method takes a NSNotification as an argument and through all of my search I have seen that you never really want to create an NSNotification object and instead want to use postNotificationName from NSNotificationCenter to pass a NSNotification object. I have been unable to get this to work properly.
Here's the relevant code I have.
- (void) viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window];
[super viewWillAppear:animated];
}
- (void) viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[super viewWillDisappear:animated];
}
- (void) keyboardWillShow: (NSNotification *)notif
{
// get our app delegate so we can access currentTextField
ScrollingAppDelegate *appDelegate = (ScrollingAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.notif = notif;
NSDictionary *info = [notif userInfo];
NSValue *aValue = [info objectForKey: UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
float bottomPoint = (appDelegate.currentTextField.frame.origin.y+ appDelegate.currentTextField.frame.size.height+10);
scrollAmount = keyboardSize.height - (self.view.frame.size.height - bottomPoint);
if (scrollAmount > 0)
{
moveViewUp = YES;
[self scrollTheView:YES];
}
else
{
moveViewUp = NO;
}
}
- (void)scrollTheView: (BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = self.view.frame;
if (movedUp)
{
rect.origin.y -= scrollAmount;
}
else
{
rect.origin.y += scrollAmount;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
// declare app delegate so we can access a varibale in it.
ScrollingAppDelegate *appDelegate = (ScrollingAppDelegate *)[[UIApplication sharedApplication] delegate];
// set the app delegate variable currentTextField to the textField which just got focus so we can access it
// in our other method keyboardWillShow
appDelegate.currentTextField = textField;
// some how call keyboardWillShow here so the view will scroll to the current text field
}
If I am going about this all wrong please let me know. I have been searching the net for answers but the have eluded me so far. Everything I find about scrolling the view only handles one text field and not multiple the way I need it.
Thanks

I didn't bookmark the original post I found this in, but here's a link to the code snipet I've been using for exactly this kind of thing:
https://gist.github.com/295089

Related

how to slide screen in iphone app to look the data which we are entering in textfield [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Making the view slide up to make room for the keyboard?
Xcode/iOS5: Move UIView up, when keyboard appears
As we normally enter data in textfield keyboard appears and it hides the data what we are entering in fields so is there any way that screen should slide up so that we can see data which is entered in the fiedl.
try this code.......
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
rect.origin.y -= moveKeyboard;
}
else
{
rect.origin.y += moveKeyboard;
}
self.view.frame = rect;
[UIView commitAnimations];
}
-(void)keyboardWillShow
{
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide
{
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
Edit: moveKeyboard is float. Set its value according to your need.
There are notifications (UIKeyboard[Will|Did][Show|Hide]Notification) that tell you the keyboard is about to appear or disappear, and you can use those to trigger the code that moves your views. There are different ways to move the views -- you can move them yourself, adjusting their positions as you like; you can put them all inside a single view so that you only have to move the container; or you can embed them in a scroll view and simply adjust the content offset for the scroll view.
See Apple's document Managing the Keyboard, and in particular the section called Moving Content That is Located Under the Keyboard. There's sample code there, too, and it works quite well.

UIKeyboardWillHide not triggered

I read many post here about this topic, but I wasn't able to find an answer to my question, so, hope you won't be bored about another UIKeyboard post :-)
In my view controller's implementation I added self as an observer for the two notifications UIKeyboardWillShowNotification and UIKeyboardWillHideNotification, passing the selectors keyboardWillShow: and keyboardWillHide: to handle to notifications. As I touch a UITextField, the keyboardWillShow: method is called but when I press a "Done" button (which dismisses the keyboard) the keyboardWillHide: method is not called.
Really, I'd like to make my UITextField show a keyboard with the "hide button" on the bottom right of the keyboard, but I wasn't able to find the right keyboard type. Maybe I need to set the textfield retuntype to "...Done". In that way I saw that "return" key turns to "done".
So I set a toolbar to be my UITextField's inputAccessoryView, so now I can show a standard keyboard with a tool bar above with the "Done" button. As a user touches that button, I hide the keyboard with the resignFirstResponder method.
The strange thing is that when I call resignFirstResponder, the UIKeyboardWillHideNotification isn't posted; at least the keyboardWillHide: method is not called.
What do you suggest to me? I really wanted to display a keyboard with the small button with the down arrow to hide the keyboard, but also this solution could be right, but I'd like to resize the view and to do this I need to observer UIKeyboardWillHideNotification.
Thank you very much for help...
(ADDED:)
In viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:[[self view] window]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:[[self view] window]];
I took these declarations from one of "yours" post :-) But the willShow works...
The action of the "Done" button that's in the UIToolbar that's assigned to be the inputAccessoryView of my text field is:
-(void)keyboardDone {
[msgTextField resignFirstResponder];
CLOSED:
OK! When a developer is stupid... it is stupid :-) :-)
This is my corrected willHide method:
-(void)keyboardWillHide:(NSNotification*)n {
NSDictionary* userInfo;
CGSize keyboardSize;
CGRect viewFrame;
/* This was the bad guy :) I forgot to delete it
* after I previously copied the willShow method that
* checks if keyboard is already shown (if so returns).
*
* if( keyboardIsShown )
* return;
*/
userInfo = [n userInfo];
keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
viewFrame = [[self scrollView] frame];
viewFrame.size.height += ( keyboardSize.height - TABBAR_HEIGHT );
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.5];
[[self scrollView] setFrame:viewFrame];
[UIView commitAnimations];
keyboardIsShown = NO;
NSLog(#"HIDE\n");
}
First of all I'd like to thank you all for this useless work in helping me. I'd like to give you some points, so I'll try to rise a "interest point" for each answer, but I need to choose the right one... hard part... :-)
Excuse me again... I really didn't see the if() statement...
If you read the documents for UIWindow it says that the notification object for these notifications is nil. You are passing self.view.window in as the object to the addObserver:selector:name:object: method. Try passing nil instead:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
It's important to note that when the user hides the software keyboard via the hide button, the hide methods aren't called. The show methods are called again, but the keyboard is nearly off screen except for the home row toolbar.
Check, if keyboardDone really gets called (i.e. with NSLog(#"%#", #"keyboard done called");). If its get called, but resignFirstResponder does not help dismissing the keyboard, then try this:
[self.view endEditing:YES];
Please also provide your keyboardWillHide: method.
To set the keyboard up so that it has a "Done" button, do this:
1) Setup your view controller so that it implements the UITextFieldDelegate. For Example:
#import <UIKit/UIKit.h>
#interface TX_ViewController : UIViewController <UITextFieldDelegate>
#property (nonatomic, retain) IBOutlet UITextField *textField;
#end
2) In your view controllers implementation file, use the following code to setup the keyboard:
- (void)viewDidLoad
{
[self.textField setDelegate:self];
[self.textField setReturnKeyType:UIReturnKeyDone];
[self.textField addTarget:self action:#selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
3) And if you wish to do something when the DONE button is pressed, simply add the following function to your view controller's implementation file:
- (IBAction)textFieldFinished:(id)sender
{
[sender resignFirstResponder];
}
Also, if you are using Interface builder to create your interfaces, don't forget to setup your IBOutlet reference for the TextField; otherwise, your class won't receive the messages from the XIB.
I set this up in a sample application just to see if it works and it did perform in the way you wish for your application to perform.
Swift $
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
func keyboardWillHide(notification: NSNotification){
print("keyboardWillHide")
}

IPhones- Have the keyboard slide out with the view

We have a view open with keyboard shown , but when the back button clicked , the view slide out from right while the keyboard will slide only when the view disappeared.
if we call resignFirstResponder at viewwilldisappear, the view slide to the right while keyboard slide down at same time.
Is it possible to let the keyboard slide out with the view?
I have tested this and it works in iOS 5.1, however, I don't think this is recommended behaviour.
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows])
if ([[keyboardWindow description] hasPrefix:#"<UITextEffectsWindow"]) {
NSLog(#"%#", [keyboardWindow description]);
[UIWindow beginAnimations:#"fadeKeyboard" context:nil];
keyboardWindow.frame = CGRectMake(keyboardWindow.frame.origin.x + keyboardWindow.frame.size.width, keyboardWindow.frame.origin.y, keyboardWindow.frame.size.width, keyboardWindow.frame.size.height);
[UIWindow commitAnimations];
}
You can also use a notification UIKeyboardWillHideNotification to detect when the keyboard is going to hide, or just use the above code manually.
There is no standard way to do what you want, but...
Basically, keyboard is just a view, presented in it's own UIWindow on top of all your other windows.
So, theoretically, what you need to do is to find keyboard view and move it in desired direction. I think you should use transform property and don't mess up with frame.
Class keyboardClass = NSClassFromString(#"UIPeripheralHostView");
for ( UIWindow *window in [[UIApplication sharedApplication] windows] ) {
for ( UIView *subview in window.subviews ) {
if ( [subview isKindOfClass:keyboardClass] ) {
// that's keyboard
}
}
}
Edited:
If you're talking about UINavigationController and it's default slide animations during push / pop, then, you just need to invoke resignFirstResponder in viewDidDisappear and becomeFirstResponder in viewWillAppear on your text view. That way your keyboard will slide along with your view.
Try sticking resignFirstresponder in the viewDidDisappear method instead.
**Set notificatins and use these methods.....Hope it solve problem:
First of all set your whole view in scrollView**
-(void)keyboardDidHide:(NSNotification *)notif
{
NSTimeInterval duration = 0.4;
[UIView animateWithDuration:duration animations:
^{
scrollView.contentSize=CGSizeMake(320,scrollOriginalFrame.size.height);
}];
keyboardVisible=NO;
}
-(void)keyboardDidShow:(NSNotification *)notif
{
scrollView.contentSize=CGSizeMake(self.view.frame.size.width, scrollOriginalFrame.size.height+235);
NSTimeInterval duration = 0.4;
[UIView animateWithDuration:duration animations:
^{
[scrollView setContentOffset:CGPointMake(0,162) animated:YES];
}];
keyboardVisible=YES;
}
**In viewDidLoad() add this**
//keyboard
scrollOriginalFrame=self.view.frame;
scrollOriginalFrame.size.height-=103;
scrollView.contentSize=scrollOriginalFrame.size;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
keyboardVisible=NO;

iPhone - Keyboard hides TextField

I am using UITextField to receive user inputs. However, since my textfields are towards the middle/bottom of the nib, it gets hidden when the keyboard pops up. Is there any way sort of slide it along with the keyboard so that it's on the top of the keyboard during selection? Also, since I am also using the numberpad, is there an easy way to include a done button somewhere?
Thanks for the help.
I have made a simple Application for this problem. It automatically checked the Textfield's position and if keyboard hides it , it will automatically move up as per need.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
int animatedDistance;
int moveUpValue = textField.frame.origin.y+ textField.frame.size.height;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = 216-(460-moveUpValue-5);
}
else
{
animatedDistance = 162-(320-moveUpValue-5);
}
if(animatedDistance>0)
{
const int movementDistance = animatedDistance;
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
To scroll when the keyboard appears, I like this tutorial from Cocoa With Love.
To dismiss the number keypad, you can put a custom "Done" button on the keypad or make an invisible button over the rest of the screen. I have done the latter with code, but this tutorial uses Interface Builder.
You'll have to do this one manually. Check out this answer.
UITextField: move view when keyboard appears
Below code works perfect for me .
[textFieldFocused becomeFirstResponder];
[self.view addSubview:startView];
[textFieldFocused resignFirstResponder];
Use this code:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:self.view.window];
}
- (void)viewDidDisappear:(BOOL)animated {
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
}
- (void)keyboardDidHide:(NSNotification *)n
{
CGRect viewFrame = scrollView.frame;
UIDeviceOrientation orientSide = [[UIDevice currentDevice] orientation];
if ((orientSide == UIDeviceOrientationLandscapeRight) || (orientSide == UIDeviceOrientationLandscapeLeft))
viewFrame.size.height += 140;
else viewFrame.size.height += 216; //portrait mode
scrollView.frame = viewFrame;
keyboardVisible = NO;
}
- (void)keyboardDidShow:(NSNotification *)n
{
CGRect viewFrame = scrollView.frame;
UIDeviceOrientation orientSide = [[UIDevice currentDevice] orientation];
if ((orientSide == UIDeviceOrientationLandscapeRight) || (orientSide == UIDeviceOrientationLandscapeLeft))
viewFrame.size.height -= 140;
else viewFrame.size.height -= 216; //portrait mode
scrollView.frame = viewFrame;
keyboardVisible = YES; }
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
CGRect viewFrame = scrollView.frame;
if ((interfaceOrientation == UIDeviceOrientationLandscapeRight) || (interfaceOrientation == UIDeviceOrientationLandscapeLeft)) //wants to change to landscape mode
if (keyboardVisible == NO)//currently in portrait,check if keyboard was present
viewFrame.size = CGSizeMake(480,250);
else viewFrame.size = CGSizeMake(480,170);
else {//wants to change to portrait mode
if (keyboardVisible == NO)//currently in landscape,check if keyboard was present
viewFrame.size = CGSizeMake(320,416);
else viewFrame.size = CGSizeMake(320,200);
}
scrollView.frame = viewFrame;
return YES;
}
Works for landscape mode too, but only for iphone. For iPad, change the frame settings accordingly. The textFieldShouldReturn: method will make the keyboard hide when return is pressed. Hope this helps...
if someone needs it, i`ve translated the solution of ValayPatel to swift
func animatedTextField(textField: UITextField, up: Bool){
var animatedDistance : Int = 0
let moveUpValue : Int = Int(textField.frame.origin.y) + Int(textField.frame.size.height)
switch UIDevice.currentDevice().orientation {
case .Portrait , .PortraitUpsideDown:
animatedDistance = 216 - (460 - moveUpValue - 5)
default:
animatedDistance = 162 - (320 - moveUpValue - 5)
}
if (animatedDistance > 0 ){
let movementDistance : Int = animatedDistance
let movementDuration : Float = 0.3
let movement = up ? -movementDistance : movementDistance
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(NSTimeInterval(movementDuration))
self.view.frame = CGRectOffset(self.view.frame, 0, CGFloat(movement))
UIView.commitAnimations()
}
}
This situation kind of sucks on the iPhone. What you are supposed to do is either design your interface in such a way that the fields are visible when the keyboard appears, or shift the field up when the keyboard appears. (And back down when you are done)
I suggest to look at some Apple apps like Contacts or Settings to see how they are dealing with this.
Also, I'm sure the iPhone HIG has something to say about it.
I know I'm a little late in answering this but, I found a very simple solution. If you use a UITableView controller rather than a UIViewController having a tableView as an object in it, this issue does not appear.
When you click the textfield that is at the bottom of the table, the keyboard pops up and the table view automatically scrolls up till the textfield that is being edited is visible.
However the auto scrolling does not work when we use the return button on the keyboard. In this scenario we have manually scroll the table up to see the textfield.
I hope this helps
I have been searching through countless answers to similar questions. For basic single screen apps, this solutions works like a charm and is incredibly simple to implement: https://github.com/michaeltyson/TPKeyboardAvoiding
Certainly there are more elegant solutions, but this will get the job done and quick.
It's simple as setting UITableView to edition mode !
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView setEditing:YES];
}
If you would like to hide a delete bubbels, to the left of a cell then implement a UITableViewDelegate Method:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"%f",textField.frame.origin.y);
CGPoint scrollPoint = CGPointMake(0, textField.frame.origin);
[scrollView setContentOffset:scrollPoint animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[scrollView setContentOffset:CGPointZero animated:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
Please use this code in your view controller.m file ..Using this code when you click the text field the keyboard will appear.Again when you click your view the keyboard will be hide ..I hope this is helps you...

tableView resize problem when pushing another view while keyboard is showing

I have a tableview controller under a navigation controller. Some of my table cells contain text fields, so when I tap on them, a keyboard will show up and automatically resize (shrink) the bounds of my tableview. The bounds is then restored when the keyboard is dismissed programmatically by calling resignFirstResponder on my text field.
Some of my cells would push a new view controller into the view stack when tapped on, so I first resign my current textfield before pushing the view controller:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (currentEditingTextField != nil) {
[currentEditingTextField resignFirstResponder];
currentEditingTextField = nil;
}
return indexPath;
}
The problem is when I navigate back to my table view, the bounds of the tableview is sized as if the keyboard is still there. I know this because the scroll indicator only reaches right above where the keyboard was and there is empty view space below the table view.
Anybody experienced this and know of a solution? Thanks
I had the same issue. I found that I need to prevent call [UIView viewWillDisappear:] before the keyboard hides.
My solutions for this.
// useful method, thus I don't need to remember current first responder
#interface UIView (FindAndResignFirstResponder)
- (BOOL)findAndResignFirstResonder;
#end
// ---
#implementation UIView (FindAndResignFirstResponder)
// http://stackoverflow.com/questions/1823317/how-do-i-legally-get-the-current-first-responder-on-the-screen-on-an-iphone
- (BOOL)findAndResignFirstResonder {
if (self.isFirstResponder) {
return [self resignFirstResponder];
}
for (UIView *subView in self.subviews) {
if ([subView findAndResignFirstResonder]) {
return YES;
}
}
return NO;
}
#end
// ---
#interface MyTableViewController : UITableViewController {
// some booleans required to track state of keyboard and view
BOOL hidingKeyboard;
BOOL viewWillDisappear;
BOOL viewWillDisappearAnimated;
}
// methods for keyboard event handling
- (void)keyboardWillHide:(id)sender;
- (void)keyboardDidHide:(id)sender;
#end
// ---
#implementation MyTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// adding observer for keyboard events (notifications)
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
hidingKeyboard = NO;
viewWillDisappear = NO;
}
- (void)viewDidUnload {
[super viewDidUnload];
// removing observer
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewWillDisappear:(BOOL)animated {
// finding and resigning first responder
[self.view findAndResignFirstResonder];
if (hidingKeyboard) {
// if keyboard hide animation in process,
// remembering to run [super viewWillDisappear:] after keyboard hides
viewWillDisappear = YES;
viewWillDisappearAnimated = animated;
} else {
// if there is no keyboard hide animation,
// calling super immediately
[super viewWillDisappear:animated];
}
}
- (void)keyboardWillHide:(id)sender {
hidingKeyboard = YES;
}
- (void)keyboardDidHide:(id)sender {
hidingKeyboard = NO;
if (viewWillDisappear) {
// calling [super viewWillAppear:] after keyboard hides, if required
viewWillDisappear = NO;
[super viewWillAppear:viewWillDisappearAnimated];
}
}
#end