Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get keyboard size without UIKeyboardWillShowNotification

I want to get the size of the keyboard without using the keyboard notifications available. The reason is I have several text field on the view and I don't need to resize the view for all of them, as I have seen in nearly every example. I just need to resize the view for some of the text fields/views that will be behind the keyboard when editing. So I'm using textFieldDidBeginEditing and textFieldDidEndEditing methods, because here I know what textfield is being edited. Another problem is that even if I subscribe to the keyboard notifications the UIKeyboardWillShowNotification is fired after textFieldDidBeginEditing so I cannot get the keyboard size on the first activation. I assume no information is provided from the keyboard notification functions where the actual text field or view is abailable.

The following code works but I need the keyboard size:

- (void) textFieldDidBeginEditing:(UITextField *) textField {
  if ([theControls containsObject: textField]) {
    [UIView beginAnimations: @"szar" context: nil];
    [UIView setAnimationDuration:0.3];
    self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, -216);
    [UIView commitAnimations];
  }
}

- (void) textFieldDidEndEditing:(UITextField *) textField {
  if ([theControls containsObject: textField]) {
    [UIView beginAnimations: @"szar" context: nil];
    [UIView setAnimationDuration:0.3];
    self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, +216);
    [UIView commitAnimations];
  }
}
like image 903
gyozo kudor Avatar asked Aug 02 '11 09:08

gyozo kudor


1 Answers

I still used Notifications but not the ones you specified against. Maybe this will work better? Just trying to help, I understand how frustrating these things can be.

viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWasShown:)
                                         name:UIKeyboardDidShowNotification
                                       object:nil];
 //For Later Use
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWillHide:)
                                         name:UIKeyboardWillHideNotification
                                       object:nil];
}

- (void)keyboardWasShown:(NSNotification *)notification {
  // Get the size of the keyboard.
  CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
}

Edit: This May Help As Well To Distinguish Active Text Fields From The Non-Active

- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.activeTextField = textField;
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
self.activeTextField = nil;
}
like image 159
rich Avatar answered Oct 05 '22 07:10

rich