2

I am working on a chat app wherein I have a textview (not textfield) and when I click on it, the keyboard should show and everything should move up.

Till now, I have managed to shift the frame of the table view and textview up and show the keyboard using the below code.

- (void)keyboardWasShown:(NSNotification *)notification {

    NSDictionary* info = [notification userInfo];

    keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGPoint contentViewOrigin = self.contentView.frame.origin;

    CGFloat contentViewHeight = self.contentView.frame.size.height;

    CGRect visibleRect = self.view.frame;

    visibleRect.size.height -= keyboardSize.height;
    BOOL up = CGRectContainsPoint(visibleRect, contentViewOrigin);

    if (!up){


    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,self.tableView.frame.origin.y,self.tableView.frame.size.width,280.0f);


    self.contentView.frame = CGRectOffset(self.contentView.frame, 0, 0 - keyboardSize.height);

    if([self.tableView numberOfRowsInSection:0]!=0)
    {
        NSIndexPath* ip = [NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:0]-1 inSection:0];
        [self.tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:UITableViewRowAnimationLeft];
    }


}

}

- (void)keyboardWillBeHidden:(NSNotification *)notification {

self.contentView.frame = originalContentView;
self.tableView.frame = originalTable;
}


- (void)registerForKeyboardNotifications {

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

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

}

- (void)deregisterFromKeyboardNotifications {

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardDidHideNotification
                                              object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillHideNotification
                                              object:nil];

}

But when I saw how whatsapp does it, mine looked like a hack. Whatsapp's keyboard moves up together with all the elements while mine works like this: First the keyboard is shown, a notification is sent to the app, notification is received, the code calculates the height of keyboard and moves up the elements according to the height.

I have searched and found the solution that I have implemented.

Can someone help??

kerry
  • 2,362
  • 20
  • 33

2 Answers2

3

I use this trick a lot in my apps. You want to listen to UIKeyboardWillShowNotification and UIKeyboardWillHideNotification. The best way to handle animation in my opinion is using autolayout. When you call [self.view layoutIfNeeded]; your views will move along with the keyboard animation. No animation block needed.

I've set up a simple project for anyone to try and see how it works!

- (void)addKeyboardNotificationsObserver {

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

}

- (void)handleKeyboardWillShow:(NSNotification *)paramNotification

{

    NSDictionary* info = [paramNotification userInfo];

    //when switching languages keyboard might change its height (emoji keyboard is higher than most keyboards). 
    //You can get both sizes of the previous keyboard and the new one from info dictionary. 

    // size of the keyb that is about to disappear
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    // size of the keyb that is about to appear
    CGSize kbSizeNew = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    //make adjustments to constraints here...

    //and here where's magick happen!

    [self.view layoutIfNeeded];

}

- (void)handleKeyboardWillHide:(NSNotification *)paramNotification

{
    //adjust constraints

    [self.view layoutIfNeeded];

}
NKorotkov
  • 3,591
  • 1
  • 24
  • 38
1
  1. You can use UIKeyboardWillShowNotification and UIKeyboardWillHideNotification
  2. Try TPKeyboardAvoidingScrollView from here: https://github.com/michaeltyson/TPKeyboardAvoiding (my choice)
  • but by using UIKeyboardWillShowNotification, will the keyboard size be available before keyboard is fully shown? I mean, will keyboard size variable will change dynamically to give me the present value of keyboard size height? – kerry Jun 05 '15 at 10:32
  • in this case just use option 2 – Ilyas Sirayev Jun 09 '15 at 10:48