点击键盘,控件上移的方式
来源:互联网 发布:linux 清空cache 编辑:程序博客网 时间:2024/05/05 19:38
点击键盘,被挡住的控件的上移,回收键盘,控件回到原处,要实现这个功能,共有两种方法
一:(通知中心,监听键盘的弹起和回收)
1.监听键盘的弹起
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
2.监听弹起的时候的触发方法,用UIView的动画效果实现
// 键盘弹起的时候触发方法- (void)keyBoardWillAppear:(NSNotification *)notification{ NSLog(@"键盘弹起了"); // 找到键盘的尺寸 CGRect rect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];#warning 整体打印结构体的方法 NSLog(@"%@", NSStringFromCGRect(rect));#warning 用动画效果让改变view的frame // 用UIView的动画,让视图随键盘向上平移 [UIView animateWithDuration:0.2 animations:^{ self.myView.frame = CGRectMake(100, 600 - rect.size.height, 200, 50); }];}
3.监听键盘的回收
// 监听键盘的回收 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
4.键盘回收的时候实现的方法
(1)点击空白回收键盘
// 点击空白处回收键盘- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [super touchesBegan:touches withEvent:event]; [self.textField resignFirstResponder];}
(2)控件回到原处(view的动画效果, 可以再点击空白处回收键盘里实现,注意时间需要改成0.2)
// 让view回到原来的位置- (void)keyBoardWillHidden:(NSNotification *)notification{ // 也可以在touch里写(在touch的方法里写,时间需要注意) [UIView animateWithDuration:2 animations:^{ self.myView.frame = CGRectMake(100, 600, 200, 50); }];}
二:textField的协议方法
// 监控当前的状态- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ NSLog(@"编辑开始"); CGFloat height = textField.center.y - HEIGHT / 2; if (height > 0) { self.view.center = CGPointMake(self.view.center.x, HEIGHT / 2 - height); } return YES;}// 结束编辑- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ CGFloat heigth = textField.center.y - HEIGHT / 2; if (heigth > 0) { self.view.center = CGPointMake(self.view.center.x, self.view.center.y + heigth); } return YES;}
0 0
- 点击键盘,控件上移的方式
- iOS TextField输入框点击键盘时随着键盘上移
- IOS 文本输入时,文本框上移。 点击空白区域 取消键盘的代码
- html页面在手机中点击输入框调出键盘页面内容上移的粗暴解决办法
- 点击return键关闭键盘的方式
- 实现view底部控件随着键盘的弹出而上移的效果
- 控件上移 之 虚拟键盘挡住UITextFiled控件
- 点击textField,监听键盘事件实现页面上移
- 键盘的弹出引起的页面上移
- TextField随着键盘的弹出上移
- 键盘弹起布局上移的问题
- Cordova iOS 应用在第三方输入法的键盘弹出(点击输入框)时,页面不上移,导致输入框被键盘遮挡 的解决办法
- iOS键盘弹出的时候会挡住底部的控件,可以通过以下方法将整个UIView上移。
- 点击控件隐藏键盘操作
- Android中RecyclerView的item中控件的点击事件添加删除一行、上移下移一行的代码实现
- Android中RecyclerView的item中控件的点击事件添加删除一行、上移下移一行的代码实现
- iOS开发记录——点击输入框上移屏幕,点击空白处隐藏键盘
- view随键盘上移
- android开发之让Android点击通知栏信息后返回正在运行的程序
- JQuery中操作Css样式的方法
- 前端编辑器哪个好
- python练习-itertools模块
- 桌面气泡通知 (Desktop notifications )
- 点击键盘,控件上移的方式
- 深入浅出JMS(一)--JMS基本概念
- Android系统之路(初识MTK) ------ Default Voice Control/输入法校准、自动校准、首字母大写、按键声音
- Activity intent经常使用的 FLAG 集合
- HBase设计与开发
- 65条常用正则表达式
- 华为机试——字符串中括号是否匹配
- navicat工具把SQL Server数据库转换MySQL数据库
- Android 打开页面加载数据时的遮罩弹屏效果