在iOS虚拟键盘上添加动态隐藏按钮
来源:互联网 发布:中医之钥 知乎 编辑:程序博客网 时间:2024/04/28 00:51
最近两周和团队做一个关于地理围栏技术的公交实时查询项目,为了给用户比较良好的交付,想在键盘上添加一个按钮,实时根据键盘不同高度变换按钮位置,再不做输入的时候点击按钮能够隐藏键盘,这种方式在很多软件上都有体现,然后在网上查阅了关于检测键盘高度一些相关知识,以下是一个Demo,代码有很多需要优化地方,仅供需要者参考;
先看效果:
首先是我们在ViewDidLoada()中注册了两个通知,[NSNotificationCenterdefaultCenter],检测键盘动态,一个是键盘将要弹出的时候,另一个是键盘将要退出时候键盘的信息
- (void)viewDidLoad{ NSLog(@"%@",NSStringFromSelector(_cmd)); [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];}
检测键盘消息一个六种,根据字面意思差不多都能说明函数作用
UIKeyboardWillShowNotification 通知将要发布时候显示键盘
UIKeyboardDidShowNotification 通知发布后立即显示键盘
UIKeyboardWillHideNotification 通知发布前撤销键盘
UIKeyboardDidHideNotification 通知发布后撤销键盘
UIKeyboardWillChangeFrameNotification 通知发布前迅速变化的框架的键盘。
UIKeyboardDidChangeFrameNotification 通知发布后立即改变在键盘的框架。
NSLog(@"%@",NSStringFromSelector(_cmd));是我特意加上去的,它能在控制台显示打印出当前程序所调用的函数,我在下面每个函数都加了这一句,当我进行不同操作的时候,打印出被调用函数名,在调试程序时候比较适用吧;
注册消息通知后,实现通知所响应的方法
- (void)handleKeyboardDidShow:(NSNotification *)notification { NSLog(@"%@",NSStringFromSelector(_cmd)); NSDictionary *info = [notification userInfo]; CGRect keyboardFrame; [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size; CGFloat distanceToMove = kbSize.height; NSLog(@"---->动态键盘高度:%f",distanceToMove); if (exitButton == nil) { exitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; CGRect exitBtFrame = CGRectMake(self.view.frame.size.width-40, self.view.frame.size.height - distanceToMove, 40.0f, 30.0f); exitButton.frame = exitBtFrame; [exitButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateNormal]; [self.view addSubview:exitButton]; } exitButton.hidden=NO; [self adjustPanelsWithKeyBordHeight:distanceToMove]; [exitButton addTarget:self action:@selector(CancelBackKeyboard:) forControlEvents:UIControlEventTouchDown]; }
在这个函数方法中值得探讨的是关于键盘所包含信息,因为每一次键盘弹出的时候也是动画形式弹出,他的坐标位置大小包含在userInfo的字典中,现在我用
NSLog(@"-->info:%@",info);打印出info对象,这些信息都可以在不同存储类型,取值的时候注意取值方式,此处只是提一提,希望以后有时间在做探讨,
- (void)handleKeyboardWillHide:(NSNotification *)notification { NSLog(@"%@",NSStringFromSelector(_cmd)); if (exitButton.hidden==NO) { exitButton.hidden = YES; } // if (exitButton.superview) // {// [exitButton removeFromSuperview];// [exitButton release];// } }
-(void)adjustPanelsWithKeyBordHeight:(float) height{ NSLog(@"%@",NSStringFromSelector(_cmd)); if (exitButton) { CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 40, self.view.frame.size.height - height-30, 40.0f, 30.0f); exitButton.frame = exitBtFrame; [self.view addSubview:exitButton]; } // UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];// if (exitButton.superview == nil) // {// [tempWindow addSubview:exitButton];// // 注意这里直接加到window上// } }
-(void)CancelBackKeyboard:(id)sender{ NSLog(@"%@",NSStringFromSelector(_cmd)); [textField resignFirstResponder]; }- (void)viewDidUnload{ [self setTextField:nil]; exitButton=nil; [super viewDidUnload]; // Release any retained subviews of the main view.}- (void)dealloc { [textField release]; [exitButton release]; [[NSNotificationCenter defaultCenter] removeObserver:self];//移除所注册的通知 [super dealloc];}
源代码:http://download.csdn.net/detail/duxinfeng2010/4831311
- 在iOS虚拟键盘上添加动态隐藏按钮
- 在iOS虚拟键盘上添加动态隐藏按钮
- 在iOS虚拟键盘上添加动态隐藏按钮
- 在iOS虚拟键盘上添加动态隐藏按钮
- iOS虚拟键盘上添加动态按钮
- iOS虚拟键盘上添加动态按钮
- swift --->iOS 虚拟键盘动态添加视图
- ios 隐藏虚拟键盘
- iOS隐藏键盘————给iOS键盘添加收起(隐藏)按钮
- ios WKWebview弹出键盘上的按钮隐藏
- ios下隐藏虚拟键盘
- 隐藏键盘,在键盘上部添加一个取消键盘的按钮
- iOS 虚拟键盘添加view
- ios在 键盘左下角添加“完成”按钮的实现原理
- iOS 动态添加按钮
- IOS细节之虚拟键盘隐藏事件添加与删除bug
- iOS UISeachBar键盘消失 虚拟按钮
- iOS 上常用的两个功能:点击屏幕和return退出隐藏键盘和解决虚拟键盘
- Windows 8 Metro中GridView的Group化和SemanticZoom控件的配合使用
- maven source 1.3 中不支持泛型 解决办法
- HEVC中的帧间预测
- crontab 常见 /dev/null 2>&1 详解
- flash与js跨域调用
- 在iOS虚拟键盘上添加动态隐藏按钮
- 带游标的存储过程的使用
- 获取选中的第一个radio的值
- VIM分屏功能
- Ajax入门的例子
- 读JSE源码(三)集合之TreeSet
- HDU 1012 uCalculate e
- 安装配置Django开发环境(Eclipse + Pydev)
- ASPxGridView 结合CheckBox实现多选(在后台事件代码控制)