【问题标题】:ios keyBoardShow with UITableViewios keyBoardShow 与 UITableView
【发布时间】:2014-09-13 02:18:06
【问题描述】:

在我的代码中,每次弹出键盘时,tableView 都会向上推,所以我可以看到 tableView 的底部。但是前三个或四个单元格将隐藏在视图后面。键盘退出时,scrollView 的顶部似乎被剪切了。

我怎样才能让我的表格视图在发短信时完整查看?

这是我的代码...

-(void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated]; 
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyboardShow:(NSNotification *)note{
  CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  CGFloat deltaY=keyBoardRect.size.height;

  [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
  self.view.transform=CGAffineTransformMakeTranslation(0, -deltaY);
  }];
}
-(void)keyboardHide:(NSNotification *)note{
  [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
    self.view.transform = CGAffineTransformIdentity;
  }];
}

向单元格添加文本后滚动到底部。

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(self.data.count-1) inSection:0];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

【问题讨论】:

    标签: ios uitableview keyboard


    【解决方案1】:

    与其在键盘显示时变换视图,不如通过调整其框架来缩小它:

    CGRect newFrame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height-deltaY);
    self.view.frame = newFrame; 
    

    【讨论】:

    • 是的,你是对的。我认为您的意思是改用“CGRectMake”。它的作品:) 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    相关资源
    最近更新 更多