SnK
SnK

Reputation: 528

Event for dismissing onscreen keyboard by clicking the keyboard button

I've been looking everywhere... maybe i'm not using the right searchwords since i do believe this is a common question.

Is there an event i can handle for when the user dismisses the keyboard by clicking the button to lower the keyboard.

The red marked button

i move a view up when a uitextfield becomes firstresponder but i want to move it down again when this button is tapped

Upvotes: 1

Views: 644

Answers (3)

maddy
maddy

Reputation: 4111

By using NSNotificationCenter you get Keyboard events.You can register for keyboard events in viewWillAppear and don't forget to unregister in viewWillDisapper.
We will use here two notifications :

  1. UIKeyboardDidShowNotification apple docs
  2. UIKeyboardDidHideNotification apple docs

    You can do some thing like this:

    • (void) viewWillAppear:(BOOL)animated {
      [super viewWillAppear:animated];
      NSLog(@"Registering for keyboard events");

    // Register for the events
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];

    // Setup content size scrollview.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT);//for e.g. (320,460)

    //Initially the keyboard is hidden keyboardVisible = NO;//in .h declare BOOL keyboardVisible; }

-(void) viewWillDisappear:(BOOL)animated {
NSLog (@"Unregister for keyboard events");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void) keyboardDidShow: (NSNotification *)notif {
NSLog(@"Keyboard is visible");
// If keyboard is visible, return
if (keyboardVisible) {
NSLog(@"Keyboard is already visible. Ignore notification.");
return;
}

// Get the size of the keyboard.
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;

// Save the current location so we can restore
// when keyboard is dismissed
offset = scrollview.contentOffset; //in .h declare CGPoint offset and UIScrollView *scrollview.;

// Resize the scroll view to make room for the keyboard
CGRect viewFrame = scrollview.frame;
viewFrame.size.height -= keyboardSize.height;
scrollview.frame = viewFrame;

CGRect textFieldRect = [activeField frame];//in .h UITextField *activeField;
textFieldRect.origin.y += 10;
[scrollview scrollRectToVisible:textFieldRect animated:YES];    

// Keyboard is now visible
keyboardVisible = YES;

}

-(void) keyboardDidHide: (NSNotification *)notif {
// Is the keyboard already shown
if (!keyboardVisible) {
NSLog(@"Keyboard is already hidden. Ignore notification.");
return;
}

// Reset the frame scroll view to its original value  
scrollview.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT);  

// Reset the scrollview to previous location
scrollview.contentOffset = offset;

// Keyboard is no longer visible
keyboardVisible = NO;

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
hope would help you :)

Upvotes: 0

Novarg
Novarg

Reputation: 7440

Try using notifications. Add that to your viewDidLoad:

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

and then create a method called keyboardWillHide:

- (void)keyboardWillHide:(NSNotification *)notification {
  //do whatever you need
}

Hope it helps

Upvotes: 1

Jason
Jason

Reputation: 4322

Check out the second paragraph in the "Managing Keyboard" section: http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UITextField_Class/Reference/UITextField.html

Upvotes: 0

Related Questions