自适应UITextView和键盘位置
来源:互联网 发布:排序算法有几种 编辑:程序博客网 时间:2024/05/29 01:52
在UITextView输入时视图被键盘挡住,这可是非常常见的事,这里说一个解决方案。
基本思想就是在键盘将要出现时设置UITextView的frame在弹出的键盘之上,在键盘将要消失时重置UITextView的位置。
首先在消息中心中注册自己为事件观察者:
#pragma mark - View life cycle- (void)viewDidLoad { [super viewDidLoad]; self.textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;}- (void)viewDidAppear:(BOOL)animated { // 当键盘出现时发送消息给self [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; // 当键盘消失时发送消息给self [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];}- (void)viewDidDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];}
然后是对应的方法:
#pragma mark - Keyboard management- (void)keyboardWillShow:(NSNotification *)noti { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; CGSize keyboardSize = [[[noti userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; CGFloat height = keyboardSize.height; self.textView.frame = CGRectMake(0.0, 20.0, self.view.bounds.size.width, self.view.bounds.size.height - height - 20.0); [UIView commitAnimations];}- (void)keyboardWillHide:(NSNotification *)noti { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.1]; self.textView.frame = CGRectMake(0.0, 20.0, self.view.bounds.size.width, self.view.bounds.size.height - 20.0); [UIView commitAnimations];}为了让键盘出现得自然点,这里加了点动画。
也可以将其写成宏,让代码更加简洁:
#define TEXTVIEW_DEFAULT_FRAME CGRectMake(0.0, 20.0, self.view.bounds.size.width, self.view.bounds.size.height - 20.0)#define ADJUST_TEXTVIEW_FRAME(nofi) \CGRectMake(0.0, 20.0, \self.view.bounds.size.width, \self.view.bounds.size.height -\[[[noti userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height - 20.0)
TEXTVIEW_DEFAULT_FRAME表示默认情况下的位置,ADJUST_TEXTVIEW_FRAME表示键盘出现后进行调整的位置。
修改后代码为:
#pragma mark - Keyboard management- (void)keyboardWillShow:(NSNotification *)noti { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; self.textView.frame = ADJUST_TEXTVIEW_FRAME(noti); [UIView commitAnimations];}- (void)keyboardWillHide:(NSNotification *)noti { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.1]; self.textView.frame = TEXTVIEW_DEFAULT_FRAME; [UIView commitAnimations];}
尝试下运行:
不足之处:
这里UITextView中隐藏键盘的方法是UIScrollViewKeyboardDismissModeInteractive,在隐藏键盘时如果有下滑手势,那么在TextView和键盘之间将会出现一段空白:
0 0
- 自适应UITextView和键盘位置
- UITextView根据键盘自适应边框
- UILabel和UITextView自适应高度
- UILabel和UITextView自适应高度
- uilabel 和uitextview 自适应大小
- UILabel和UITextView自适应高度
- UITextView自定义键盘和系统键盘
- iOS开发TextField根据键盘自适应位置
- 自动根据键盘位置调整UITextView的高度
- UITextView 键盘
- iOS-UITextField和UITextView隐藏键盘
- UITextView 的创建和键盘禁用
- iOS-UITextField和UITextView隐藏键盘
- IOS UItextfield 和 UItextView 键盘隐藏
- UITextView和UITextField退出键盘的方式
- iOS UITextView和UITextField的键盘处理
- UITextView的使用以及打开键盘的时候自动调整UITextView的位置
- iOS开发笔记--UILabel和UITextView自适应文本高度代码
- <zz>计算机视觉和模式识别领域SCI期刊介绍
- DB2查看配置
- 《设计模式》读书笔记:策略模式
- nyoj58 最少步数
- OSG在WIN与VS中配置与测试
- 自适应UITextView和键盘位置
- c++11 std - nullptr
- linux下安装,卸载,以及查看安装的软件
- android 设置录制视频的时长与大小限制,超时、超大小时自动停止
- mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' 解决方法
- Windows 系统盘已使用空间超过实际大小的原因与解决方法
- 屌丝程序员的故事-加薪后的苦恼
- MFC SendMessage()函数传递字符串
- Activity的Launch mode详解 singleTask正解