Advertisement
Guest User

Untitled

a guest
May 29th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. //UIView *_currentText;//当前编辑的textField
  2. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  3. _currentText = textField;
  4. }
  5. - (void)keyboardWillShow:(NSNotification *)aNotification {
  6. //键盘高度
  7. NSDictionary* info = [aNotification userInfo];
  8. //kbSize即為鍵盤尺寸 (有width, height)
  9. CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
  10. UITableViewCell *cell = (UITableViewCell *)[_currentText superview];
  11. //将tableView里面的这个cell的Frame转化成tableView的父视图的相对位置
  12. //即cell在tableView的父视图的位置
  13. CGRect rect = [_table convertRect:cell.frame toView:[_table superview]];
  14. /*KHFullScreenHeight全屏高度
  15. * 64导航栏的加状态栏的高度
  16. * kbSize.height 键盘的高度
  17. * CGRectGetMaxY(rect) cell在tableView的父视图的位置的y+cell本身的height
  18. */
  19. CGFloat cj = (KHFullScreenHeight-64-kbSize.height) - (CGRectGetMaxY(rect));
  20.  
  21. if (cj < 0) {
  22. //被键盘遮住
  23. //tableView上移动
  24. //cj差多少就移动多少
  25. [UIView animateWithDuration:0.3 animations:^{
  26. self.table.frame = CGRectMake(0, self.table.frame.origin.y+cj, self.table.frame.size.width, self.table.frame.size.height);
  27. }];
  28. }
  29. }
  30. - (void)keyboardWillHide {
  31. //tableView回到原本位置
  32. [UIView animateWithDuration:0.3 animations:^{
  33. self.table.frame =CGRectMake(0, 0, self.table.frame.size.width, self.table.frame.size.height);
  34. }];
  35. }
  36.  
  37. //return键收起键盘
  38. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  39. [textField resignFirstResponder];
  40. return YES;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement