我正在开发一个聊天应用程序,其中有UITableView
and a UIView
含有一个UITextField
and a 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)];
}
这将处理您所有的键盘情况,例如向上或向下或使用预测文本栏更改其框架时
当你离开你的视野时,也会删除观察者
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)