10

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

应用截图

4

3 回答 3

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];
于 2013-09-06T01:31:03.787 回答
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];
}

Methion 在一个键盘动画期间将收到多个事件。

于 2013-09-06T02:34:02.700 回答
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]; 

同样的事情,但是当键盘消失时将 -= 更改为 += 。

于 2013-09-06T00:49:03.707 回答