-2

I am a complete newbie in Qbjectives-C.

I saw this question had been asked before but I didn't quite understand it from the answers provided there.

Abizern
  • 146,289
  • 39
  • 203
  • 257

2 Answers2

-1

You need to add notifications to catch the keyboard status. In the notification you can capture the keyboard size and move the view according to the keyboard size.

#define kKeyboardAnimationDuration 0.3
@interface YourViewController:UIViewController
{
    BOOL keyboardIsShown;
}

And then in your implementation

- (void)viewDidLoad
{
// register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification 
                                               object:self.view.window];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification 
                                               object:self.view.window];

    keyboardIsShown = NO;
}

- (void)viewDidUnload
{
[super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIKeyboardWillShowNotification 
                                                  object:nil]; 
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIKeyboardWillHideNotification 
                                                  object:nil]; 
}

#pragma mark - Keyboard Events
- (void)keyboardWillShow:(NSNotification *)n
{
    if (keyboardIsShown) 
    {
        return;
    }

    NSDictionary* userInfo = [n userInfo];
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y-= (keyboardSize.height);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:kKeyboardAnimationDuration];
    [self.view setFrame:viewFrame];
    [UIView commitAnimations];

    keyboardIsShown = YES;
}

- (void)keyboardWillHide:(NSNotification *)n
{
    NSDictionary* userInfo = [n userInfo];
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y+= (keyboardSize.height);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:kKeyboardAnimationDuration];
    [self.view setFrame:viewFrame];
    [UIView commitAnimations];

    keyboardIsShown = NO;
}
PgmFreek
  • 6,374
  • 3
  • 36
  • 47
  • Do I put the first code snippet in the .h file and the second longer bit in the .m file? – user2731202 Nov 18 '13 at 09:52
  • yes. You can declare the variables(keyboardIsShown) and macros(kKeyboardAnimationDuration) in .h file and you should register and use the notification in the .m file – PgmFreek Nov 18 '13 at 09:55
-2

Use scroll view to push your content up when keyboard appears.

Use this, "TPKeyboardAvoidingScrollView", from:

https://github.com/michaeltyson/TPKeyboardAvoiding

Works real good.