在iOS虛擬鍵盤上添加動態隱藏按鈕

  最近兩週和團隊做一個關於地理圍欄技術的公交實時查詢項目,爲了給用戶比較良好的交付,想在鍵盤上添加一個按鈕,實時根據鍵盤不同高度變換按鈕位置,再不做輸入的時候點擊按鈕能夠隱藏鍵盤,這種方式在很多軟件上都有體現,然後在網上查閱了關於檢測鍵盤高度一些相關知識,以下是一個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

發佈了9 篇原創文章 · 獲贊 15 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章