UITableview的底部用键盘向上移动

时间:2013-09-06 00:33:52

标签: ios objective-c cocoa-touch uitableview

我有一个带有UITableview和UITextView的应用程序,所以有时候键盘已启动。键盘启动时,我无法访问底部的3-4个表格单元格,因为它们总是卡在键盘后面。如何在显示键盘时使UITableView仅占用键盘上方的空间?

Screenshot of app

3 个答案:

答案 0 :(得分:11)

我通常使用https://github.com/michaeltyson/TPKeyboardAvoiding 哪个会自动解决您的正常问题 但是如果你想手动编写一些代码

使用表格观看次数setContentOffsetscrollToRowAtIndexPath

    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];

答案 1 :(得分:4)

我遇到了同样的问题,这是我的解决方案。希望它有所帮助。

- (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;
}

添加和删除事件侦听器。

- (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];
}

在一个键盘动画中接收多个事件的方法。

答案 2 :(得分:1)

试试这个:

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]; 

同样的事情,但改变 - = + =键盘消失的时候。