10

I have an application with a UITableview and a UITextView, so sometimes the keyboard is up. Whenever the keyboard is up, I can't access the bottom 3-4 table cells because they are always stuck behind the keyboard. How can I make the UITableView only take up the space above the keyboard while the keyboard is displayed?

Screenshot of app

Jakob Weisblat
  • 7,450
  • 9
  • 37
  • 65

3 Answers3

11

I normally use https://github.com/michaeltyson/TPKeyboardAvoiding Which will automatically solve your normal problem But if you want to do some coding manually

Use table views setContentOffset or scrollToRowAtIndexPath

    CGPoint scrollPt = CGPointMake(textFieldRect.origin.x, textFieldRect.origin.y);  
  [_tableView setContentOffset:scrollPt animated:YES];

    UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
    [_tableView scrollToRowAtIndexPath:[_tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
Bishal Ghimire
  • 2,580
  • 22
  • 37
4

I've had the same problem, and here is my solution. Hope it helps.

- (void) keyboardWillShow: (NSNotification*) aNotification
{
    [UIView animateWithDuration: [self keyboardAnimationDurationForNotification: aNotification] animations:^{
        CGSize kbSize = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
        //change the frame of your talbleiview via kbsize.height
    } completion:^(BOOL finished) {
    }];
}

- (void) keyboardDidShow: (NSNotification*) aNotification
{    
    [UIView animateWithDuration: [self keyboardAnimationDurationForNotification: aNotification] animations:^{
        CGSize kbSize = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
        //change the frame of your talbleiview via kbsize.height
    } completion:^(BOOL finished) {
    }];
}

- (void) keyboardWillDisappear: (NSNotification*) aNotification
{
    [UIView animateWithDuration: [self keyboardAnimationDurationForNotification: aNotification] animations:^{
        //restore your tableview
    } completion:^(BOOL finished) {
    }];
}

- (NSTimeInterval) keyboardAnimationDurationForNotification:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    NSValue* value = [info objectForKey: UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval duration = 0;
    [value getValue: &duration];

    return duration;
}

Add and remove event listeners.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(keyboardWillShow:)
                                                 name: UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(keyboardDidShow:)
                                                 name: UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(keyboardWillDisappear:)
                                                 name: UIKeyboardWillHideNotification object:nil];
    }
- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardWillShowNotification object: nil];
    [[NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardDidShowNotification object: nil];
    [[NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardWillHideNotification object: nil];
}

Methion that multi events will be received during one keyboard animation.

MasterBeta
  • 606
  • 6
  • 15
1

Try this:

CGRect frame = [tableViewer frame]; //assuming tableViewer is your tableview
frame.size.height -= 200; //200 may be a bit off, should be height of keyboard
[tableViewer setFrame:frame]; 

And the same thing but change -= to += for when the keyboard disappears.

Lugubrious
  • 380
  • 1
  • 3
  • 16
  • This is the only thing that worked in my case - Thank You! one question - when the keyboard opens there is a black background through the "open animation" how can I remove/change that – liv a May 09 '15 at 16:32
  • A black background to the tableview? – Lugubrious May 10 '15 at 10:18
  • not to the table view, just to the "rectangle" in which the keyboard is moving up – liv a May 10 '15 at 11:03
  • Oh I see. Make the snippet run after the animation, not before. Alternatively, change the background color of the view to be white. – Lugubrious Aug 08 '15 at 02:17