1

I have a chat view with UITableView on all of the screen and on the bottom of the screen there is a UIView that holds 2 UIButtons and UITextField.

What I want to do is when the user clicks on the UITextField and the keyboard shows, the view will change his location to the top of the keyboard. For some reason I think that the AutoLayout is preventing it from moving, how could I know?

This is what I did so far:

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(kbSize.height, 0.0, kbSize.height, 0.0);
    self.tableChat.contentInset = contentInsets;
    self.tableChat.scrollIndicatorInsets = contentInsets;

    CGRect inputTextView = self.accessoryViewForChat.frame;
    inputTextView.origin.y = inputTextView.origin.y - kbSize.height;
    self.accessoryViewForChat.frame = inputTextView;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, self.activeTextField.frame.origin) ) {
        [self.tableChat scrollRectToVisible:self.activeTextField.frame animated:NO];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    self.tableChat.contentInset = contentInsets;
    self.tableChat.scrollIndicatorInsets = contentInsets;

    CGRect inputTextView = self.accessoryViewForChat.frame;
    inputTextView.origin.y = inputTextView.origin.y + kbSize.height;
    self.accessoryViewForChat.frame = inputTextView;
}

#pragma mark UITextFieldDelegate methods

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

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.activeTextField = nil;
}
ytpm
  • 4,962
  • 6
  • 56
  • 113
  • 2
    check this....http://stackoverflow.com/questions/31356293/uitableview-and-uiview-with-keyboardwillshow/31356527#31356527 and dont forget to place your textview and buttons to bottom view – Bhavin Bhadani Aug 06 '15 at 10:58

0 Answers0