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

來源:互聯網
上載者:User

  想在鍵盤上添加一個按鈕,即時根據鍵盤不同高度變換按鈕位置,再不做輸入的時候點擊按鈕能夠隱藏鍵盤,這種方式在很多軟體上都有體現,然後在網上查閱了關於檢測鍵盤高度一些相關知識,以下是一個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對象,這些資訊都可以在不同儲存類型,取值的時候注意取值方式,此處只是提一提,希望以後有時間在做探討,

  在這一段代碼上,後面注釋了5行,因為打算當鍵盤推出的時候,按鈕從視圖上移除,或者釋放按鈕,但是都導致了應用程式崩潰,後來就沒有釋放和移除操作了

  - (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];

  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.