现在的位置: 首页 > 综合 > 正文

在iOS虚拟键盘上添加动态隐藏按钮

2018年05月05日 ⁄ 综合 ⁄ 共 4087字 ⁄ 字号 评论关闭

  最近两周和团队做一个关于地理围栏技术的公交实时查询项目,为了给用户比较良好的交付,想在键盘上添加一个按钮,实时根据键盘不同高度变换按钮位置,再不做输入的时候点击按钮能够隐藏键盘,这种方式在很多软件上都有体现,然后在网上查阅了关于检测键盘高度一些相关知识,以下是一个Demo,代码有很多需要优化地方,仅供需要者参考;


先看效果:

     


    

       


首先是我们在ViewDidLoada()中注册了两个通知,[NSNotificationCenterdefaultCenter],检测键盘动态,一个是键盘将要弹出的时候,另一个是键盘将要退出时候键盘的信息

  1. - (void)viewDidLoad  
  2. {  
  3.     NSLog(@"%@",NSStringFromSelector(_cmd));  
  4.     [super viewDidLoad];  
  5.       
  6.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];    
  7.       
  8.      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];  
  9. }  


检测键盘消息一个六种,根据字面意思差不多都能说明函数作用

UIKeyboardWillShowNotification     通知将要发布时候显示键盘 

UIKeyboardDidShowNotification     通知发布后立即显示键盘

UIKeyboardWillHideNotification       通知发布前撤销键盘

UIKeyboardDidHideNotification       通知发布后撤销键盘

UIKeyboardWillChangeFrameNotification      通知发布前迅速变化的框架的键盘。

UIKeyboardDidChangeFrameNotification      通知发布后立即改变在键盘的框架。

NSLog(@"%@",NSStringFromSelector(_cmd));是我特意加上去的,它能在控制台显示打印出当前程序所调用的函数,我在下面每个函数都加了这一句,当我进行不同操作的时候,打印出被调用函数名,在调试程序时候比较适用吧;



注册消息通知后,实现通知所响应的方法

  1. - (void)handleKeyboardDidShow:(NSNotification *)notification   
  2. {  
  3.     NSLog(@"%@",NSStringFromSelector(_cmd));  
  4.     NSDictionary *info = [notification userInfo];  
  5.     CGRect keyboardFrame;  
  6.     [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];  
  7.     CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;  
  8.     CGFloat distanceToMove = kbSize.height;  
  9.     NSLog(@"---->动态键盘高度:%f",distanceToMove);  
  10.       
  11.     if (exitButton == nil) {  
  12.         exitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  13.         CGRect exitBtFrame = CGRectMake(self.view.frame.size.width-40, self.view.frame.size.height - distanceToMove, 40.0f, 30.0f);  
  14.         exitButton.frame = exitBtFrame;  
  15.         [exitButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateNormal];  
  16.         [self.view addSubview:exitButton];  
  17.           
  18.     }  
  19.     exitButton.hidden=NO;  
  20.       
  21.     [self adjustPanelsWithKeyBordHeight:distanceToMove];  
  22.       
  23.     [exitButton addTarget:self action:@selector(CancelBackKeyboard:) forControlEvents:UIControlEventTouchDown];  
  24.       
  25.   
  26. }  

在这个函数方法中值得探讨的是关于键盘所包含信息,因为每一次键盘弹出的时候也是动画形式弹出,他的坐标位置大小包含在userInfo的字典中,现在我用

NSLog(@"-->info:%@",info);打印出info对象,这些信息都可以在不同存储类型,取值的时候注意取值方式,此处只是提一提,希望以后有时间在做探讨,

在这一段代码上,后面注释了5行,因为打算当键盘推出的时候,按钮从视图上移除,或者释放按钮,但是都导致了应用程序崩溃,后来就没有释放和移除操作了

  1. - (void)handleKeyboardWillHide:(NSNotification *)notification   
  2. {  
  3.     NSLog(@"%@",NSStringFromSelector(_cmd));  
  4.     if (exitButton.hidden==NO) {  
  5.         exitButton.hidden = YES;  
  6.     }  
  7.       
  8. //    if (exitButton.superview)   
  9. //    {  
  10. //        [exitButton removeFromSuperview];  
  11. //        [exitButton release];  
  12. //    }  
  13.   
  14.       
  15. }  
  1. -(void)adjustPanelsWithKeyBordHeight:(float) height  
  2. {  
  3.     NSLog(@"%@",NSStringFromSelector(_cmd));  
  4.     if (exitButton) {  
  5.   
  6.        CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 40, self.view.frame.size.height - height-30, 40.0f, 30.0f);  
  7.         exitButton.frame = exitBtFrame;  
  8.   
  9.         [self.view addSubview:exitButton];  
  10.   
  11.     }  
  12.       
  13.                           
  14. //    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];  
  15. //    if (exitButton.superview == nil)   
  16. //    {  
  17. //        [tempWindow addSubview:exitButton];  
  18. //        // 注意这里直接加到window上  
  19. //    }  
  20.       
  21. }  

  1. -(void)CancelBackKeyboard:(id)sender  
  2. {  
  3.     NSLog(@"%@",NSStringFromSelector(_cmd));  
  4.       
  5.     [textField resignFirstResponder];  
  6.       
  7. }  
  8.   
  9.   
  10. - (void)viewDidUnload  
  11. {  
  12.     [self setTextField:nil];  
  13.     exitButton=nil;  
  14.     [super viewDidUnload];  
  15.       
  16.     // Release any retained subviews of the main view.  
  17. }  
  18.   
  19.   
  20. - (void)dealloc {  
  21.     [textField release];  
  22.     [exitButton release];  
  23.     [[NSNotificationCenter defaultCenter] removeObserver:self];//移除所注册的通知  
  24.     [super dealloc];  
  25. }  

源代码:http://download.csdn.net/detail/duxinfeng2010/4831311

抱歉!评论已关闭.