我正在开发一个聊天应用程序,其中包含UITableView
和UIView
,其中包含UITextField
和UIButton
。当键盘出现时,我正在使用以下代码将UIView
向上移动。
(void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.2f animations:^{
CGRect frame = self.inputView.frame;
UIInterfaceOrientation orientation = self.interfaceOrientation;
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
frame.origin.y -= kbSize.width;
else
frame.origin.y -= kbSize.height;
self.inputView.frame = frame;
;
}];
}
此代码在iOS 7之前可以正常工作,但在iOS 8中
UIView
不会在键盘上方显示。有人可以建议可能的问题是什么,或者iOS 8有什么变化吗?
最佳答案
您的代码似乎是正确的,但是我更喜欢使用UIKeyboardDidChangeFrameNotification或UIKeyboardWillChangeFrameNotification,因为当预测性文本栏在查看键盘时向上或向下时,这些将告诉您键盘框架的变化。
在您的ViewDidLoad中添加此
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardFrameDidChange:)
name:UIKeyboardDidChangeFrameNotification object:nil];
然后将此方法粘贴到您的ViewController中
-(void)keyboardFrameDidChange:(NSNotification*)notification{
NSDictionary* info = [notification userInfo];
CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
[yourView setFrame:CGRectMake(0, kKeyBoardFrame.origin.y-yourView.frame.size.height, 320, yourView.frame.size.height)];
}
这将处理您所有的键盘情况,例如其向上或向下或带有预测性文本栏的框架变化时
并在离开 View 时删除观察者
关于uitableview - 出现键盘时,iOS 8 UIView不会向上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25955917/