iOS键盘显示事件处理

我正在一个聊天应用程序,其中我有一个文本视图(而不是文本框),当我点击它,键盘应该显示,一切都应该向上移动。

到目前为止,我已经设法改变表视图​​和文本框的框架,并使用下面的代码显示键盘。

- (void)keyboardWasShown:(NSNotification *)notification { NSDictionary* info = [notification userInfo]; keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; CGPoint contentViewOrigin = self.contentView.frame.origin; CGFloat contentViewHeight = self.contentView.frame.size.height; CGRect visibleRect = self.view.frame; visibleRect.size.height -= keyboardSize.height; BOOL up = CGRectContainsPoint(visibleRect, contentViewOrigin); if (!up){ self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,self.tableView.frame.origin.y,self.tableView.frame.size.width,280.0f); self.contentView.frame = CGRectOffset(self.contentView.frame, 0, 0 - keyboardSize.height); if([self.tableView numberOfRowsInSection:0]!=0) { NSIndexPath* ip = [NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:0]-1 inSection:0]; [self.tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:UITableViewRowAnimationLeft]; } } } - (void)keyboardWillBeHidden:(NSNotification *)notification { self.contentView.frame = originalContentView; self.tableView.frame = originalTable; } - (void)registerForKeyboardNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; } - (void)deregisterFromKeyboardNotifications { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } 

但是当我看到whatsapp是如何做的时候,我看起来像是一个黑客。 Whatsapp的键盘与所有元素一起上移,而我的工作是这样的:首先显示键盘,通知发送到应用程序,通知收到,代码计算键盘的高度,并根据高度向上移动元素。

我已经search并find了我已经实施的解决scheme。

有人可以帮忙吗?

我在我的应用程序中使用了这个技巧。 你想要听UIKeyboardWillShowNotificationUIKeyboardWillHideNotification 。 在我看来处理animation的最好方法是使用自动布局。 当你调用[self.view layoutIfNeeded]; 你的意见将随键盘animation一起移动。 不需要animation块。

我已经为任何人build立了一个简单的项目 ,试图看看它是如何工作的!

 - (void)addKeyboardNotificationsObserver { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } - (void)handleKeyboardWillShow:(NSNotification *)paramNotification { NSDictionary* info = [paramNotification userInfo]; //when switching languages keyboard might change its height (emoji keyboard is higher than most keyboards). //You can get both sizes of the previous keyboard and the new one from info dictionary. // size of the keyb that is about to disappear CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; // size of the keyb that is about to appear CGSize kbSizeNew = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; //make adjustments to constraints here... //and here where's magick happen! [self.view layoutIfNeeded]; } - (void)handleKeyboardWillHide:(NSNotification *)paramNotification { //adjust constraints [self.view layoutIfNeeded]; } 
  1. 您可以使用UIKeyboardWillShowNotification和UIKeyboardWillHideNotification
  2. 从这里尝试TPKeyboardAvoidingScrollView: https : //github.com/michaeltyson/TPKeyboardAvoiding (我的select)