2

I will show you an example with the well known whatsapp When you touch inside the text the keyboard pops up , so I have to move or shift all that bar up and resize the view to half, so I can still see the text that I'm typing and the send button

Phase 1: http://www.appbank.net/wp-content/uploads/2010/10/WhatsAppMessenger-18.jpg

Phase 2: http://www.onetooneglobal.com/wp-content/uploads/2011/02/onetoone_whatsapp_2.png

What would be the best way to achieve this?

PartySoft
  • 2,749
  • 7
  • 39
  • 55

2 Answers2

6


#define kOFFSET_FOR_KEYBOARD 280.0

- (void)keyboardWillHide:(NSNotification *)notif {
    [self setViewMoveUp:NO];
}


- (void)keyboardWillShow:(NSNotification *)notif{
    [self setViewMoveUp:YES];
}


- (void)textFieldDidBeginEditing:(UITextField *)textField {
    stayup = YES;
    [self setViewMoveUp:YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    stayup = NO;
    [self setViewMoveUp:NO];
}

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMoveUp:(BOOL)moveUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view
    [UIView setAnimationBeginsFromCurrentState:YES];

    CGRect rect = self.view.frame;
    if (moveUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
        // 2. increase the size of the view so that the area behind the keyboard is covered up.

        if (rect.origin.y == 0 ) {
            rect.origin.y -= kOFFSET_FOR_KEYBOARD;
            //rect.size.height += kOFFSET_FOR_KEYBOARD;
        }

    }
    else
    {
        if (stayup == NO) {
            rect.origin.y += kOFFSET_FOR_KEYBOARD;
            //rect.size.height -= kOFFSET_FOR_KEYBOARD;
        }
    }
    self.view.frame = rect; 
    [UIView commitAnimations];
}

Try this methods. Edit it according to your requirement.

Adarsh V C
  • 2,314
  • 1
  • 20
  • 37
  • ok, I used your code and it WORKED, :) I had to put instead of void , IBAction and assign the events to the textedit, I still don't know the role of keyboardWillHide and keyboardWillShow and how to use them, .., for hiding the keyboard i Used http://stackoverflow.com/questions/2586937/how-to-hide-the-keyboard-programatically-in-iphone . thank you for this wonderful solution – PartySoft Mar 29 '11 at 22:51
  • In some scenarios, Animation timings may not be perfect if we use textFieldDidBeginEditing and textFieldDidEndEditing, So we can use keyboardWillShow and keyboardWillHide to adjust that timing accordingly. This two methods fires little bit early. Anyways in your case I think textFieldDidBeginEditing and textFieldDidEndEditing is sufficient. :) – Adarsh V C Mar 30 '11 at 05:35
  • stayup = YES; What is stay up here? – Minkle Garg Sep 01 '11 at 04:44
  • 2
    Some keyboards are different sizes (Japanese input, for one). To do this the right way, you need to get the keyboard bounds from the UIKeyboardFrameEndUserInfoKey value in the notification dictionary. – davehayden Mar 10 '12 at 06:44
2

You'll want to listen to the UIKeyboardDidShowNotification and UIKeyboardDidHideNotification, and in the method that corresponds to the selector you've provided the notification center resize your views at will (typically by changing the UIView.frame property)

Zaky German
  • 14,324
  • 4
  • 25
  • 31
  • Just for the record, Apple has a great tutorial about this: https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW1 – To1ne Aug 12 '13 at 21:12