博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios如何实现被键盘遮挡时,带有textfield的tableview自动上移
阅读量:5832 次
发布时间:2019-06-18

本文共 2819 字,大约阅读时间需要 9 分钟。

最正规的办法,用通知

step 1:
在进入视图的时候添加监视:(viewDidLoad什么的)

 
  1. // Observe keyboard hide and show notifications to resize the text view appropriately.
  2.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  3.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

step 2:
在键盘动作的时候移动视图:

 
  1. - (void)keyboardWillShow:(NSNotification *)notification {
  2.    
  3.     /*
  4.      Reduce the size of the text view so that it's not obscured by the keyboard.
  5.      Animate the resize so that it's in sync with the appearance of the keyboard.
  6.      */
  7.     NSDictionary *userInfo = [notification userInfo];
  8.    
  9.     // Get the origin of the keyboard when it's displayed.
  10.     NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  11.     // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
  12.     CGRect keyboardRect = [aValue CGRectValue];
  13.     keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
  14.    
  15.     CGFloat keyboardTop = keyboardRect.origin.y;
  16.     CGRect newTextViewFrame = self.view.bounds;
  17.     newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
  18.    
  19.     // Get the duration of the animation.
  20.     NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
  21.     NSTimeInterval animationDuration;
  22.     [animationDurationValue getValue:&animationDuration];
  23.    
  24.     // Animate the resize of the text view's frame in sync with the keyboard's appearance.
  25.     [UIView beginAnimations:nil context:NULL];
  26.     [UIView setAnimationDuration:animationDuration];
  27.    
  28.     textView.frame = newTextViewFrame;
  29.     [UIView commitAnimations];
  30. }
  31. - (void)keyboardWillHide:(NSNotification *)notification {
  32.    
  33.     NSDictionary* userInfo = [notification userInfo];
  34.    
  35.     /*
  36.      Restore the size of the text view (fill self's view).
  37.      Animate the resize so that it's in sync with the disappearance of the keyboard.
  38.      */
  39.     NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
  40.     NSTimeInterval animationDuration;
  41.     [animationDurationValue getValue:&animationDuration];
  42.    
  43.     [UIView beginAnimations:nil context:NULL];
  44.     [UIView setAnimationDuration:animationDuration];
  45.    
  46.     textView.frame = self.view.bounds;
  47.    
  48.     [UIView commitAnimations];
  49. }

step 3:
在退出视图的时候注销通知
viewDidUnload:

 
  1. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  2.     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

dealloc:

 
  1. [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];

这些代码是摘自apple sample code KeyboardAccessory.
些许细节自己修改下就好了,比如那个textView

转载于:https://www.cnblogs.com/bmate/p/3193209.html

你可能感兴趣的文章
兰州青年志愿者“中西合璧”玩快闪 温暖旅客回家路
查看>>
计划10年建10万廉价屋 新西兰政府:比想象中难
查看>>
甘肃发首版《3D打印职业教育教材》:校企合作育专才
查看>>
李娜入选国际网球名人堂 成亚洲第一人
查看>>
为找好心人抚养孩子 浙江一离婚父亲将幼童丢弃公园
查看>>
晚婚晚育 近20年巴西35岁以上孕妇增加65%
查看>>
读书:为了那个美妙的咔哒声
查看>>
深入探究Immutable.js的实现机制(一)
查看>>
jsp改造之sitemesh注意事项
查看>>
智能硬件的时代,嵌入式是否已经日薄西山
查看>>
SpringBoot-Shiro使用
查看>>
iOS 9.0之后NSString encode方法替换
查看>>
解决 ThinkPHP5 无法接收 客户端 Post 传递的 Json 参数
查看>>
ASMFD (ASM Filter Driver) Support on OS Platforms (Certification Matrix). (文档 ID 2034681.1)
查看>>
CRM Transaction处理中的权限控制
查看>>
[转]linux创建链接文件的两种方法
查看>>
python ipaddress模块使用
查看>>
文件权限
查看>>
busybox里的僵尸进程为何那么多
查看>>
python debug
查看>>