首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用键盘使UITableview的底部向上移动

用键盘使UITableview的底部向上移动
EN

Stack Overflow用户
提问于 2013-09-06 08:33:52
回答 3查看 18.5K关注 0票数 10

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-06 09:31:04

我通常使用https://github.com/michaeltyson/TPKeyboardAvoiding,它会自动解决您通常遇到的问题,但如果您想手动编写一些代码

使用表视图setContentOffsetscrollToRowAtIndexPath

代码语言:javascript
运行
复制
    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];
票数 11
EN

Stack Overflow用户

发布于 2013-09-06 10:34:03

我也遇到过同样的问题,下面是我的解决方案。希望能有所帮助。

代码语言:javascript
运行
复制
- (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;
}

添加和删除事件侦听器。

代码语言:javascript
运行
复制
- (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];
}

方法是在一个键盘动画期间接收多个事件。

票数 4
EN

Stack Overflow用户

发布于 2013-09-06 08:49:04

试试这个:

代码语言:javascript
运行
复制
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]; 

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18648080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档