当键盘出现时,iOS 8 UIView不会向上移动 [英] iOS 8 UIView not moving up when keyboard appears
问题描述
我正在开发一个聊天应用,其中包含 UITableView
和一个 UIView
,其中包含 UITextField
和一个 UIButton
。当键盘出现时,我使用以下代码移动 UIView
。
I am developing a chat app which has UITableView
and a UIView
containing a UITextField
and a UIButton
in it. I am using the following code to move the UIView
up when keyboard appears.
(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
没有显示在键盘上方。
This code is working fine until iOS 7, but in iOS 8 UIView
is not displaying above the keyboard.
任何人都可以建议可能存在的问题,或者是否有任何问题在iOS 8中更改了吗?
Can anyone please suggest what could be the possible issue, or is there anything that has changed in iOS 8?
推荐答案
您的代码似乎是正确的但我更喜欢使用UIKeyboardDidChangeFrameNotification或UIKeyboardWillChangeFrameNotification,因为这些会告诉您当键盘在视图中时预测文本栏上升或下降时键盘框架发生变化。
Your code seems to be correct but i will prefer using UIKeyboardDidChangeFrameNotification or UIKeyboardWillChangeFrameNotification because these will tell you the change in keyboard frame when predictive text bar gets up or down when keyboard is in view.
在ViewDidLoad中添加此
In your ViewDidLoad add this
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardFrameDidChange:)
name:UIKeyboardDidChangeFrameNotification object:nil];
然后将此方法粘贴到ViewController中
and then paste this method in your 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)];
}
这将处理所有键盘情况,例如它的上升或下降或更改它的框架带有预测文本栏
This will handle all your keyboard cases like when its up or down or change in its frame with predictive text bar
并且当您离开视图时也会移除观察者
and also remove observer when you are leaving your view
这篇关于当键盘出现时,iOS 8 UIView不会向上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!