IOS8键盘事件未正确执行
您好,我正在开发一个iphone应用程序,我在其中使用了一些键盘事件,但这些事件在IOS8中无法正常工作,就像在以前的IOS版本中一样。这就是我的问题。我有两个文本字段。我正在收听两个事件IOS8键盘事件未正确执行,ios,scroll,keyboard,Ios,Scroll,Keyboard,您好,我正在开发一个iphone应用程序,我在其中使用了一些键盘事件,但这些事件在IOS8中无法正常工作,就像在以前的IOS版本中一样。这就是我的问题。我有两个文本字段。我正在收听两个事件(void)keyboardWillBeHidden:(NSNotification*)notification和(void)keyboardwashow:(NSNotification*)notification。在这些方法中,我根据键盘高度滚动我的内容。在以前的IOS版本中,它工作正常,但在IOS8中,它导
(void)keyboardWillBeHidden:(NSNotification*)notification
和(void)keyboardwashow:(NSNotification*)notification
。在这些方法中,我根据键盘高度滚动我的内容。在以前的IOS版本中,它工作正常,但在IOS8中,它导致了问题。无论何时我选择任何文本字段,它都会触发键盘显示
,但无论何时我选择另一个文本字段,它都会触发相同的事件,即使键盘已经显示。在以前的版本中,它只触发一次,但对于IOS 8,它会触发两次。这是我的密码
- (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:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWasShown:(NSNotification *)notification {
[LogRecords showLog:@"keyboard shown ... "];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone || (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])))
{
//scroll content here according to height of keyboard ;
}
}
- (void)keyboardWillBeHidden:(NSNotification *)notification
{
[LogRecords showLog:@"keyboard hide "];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone || (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])))
{
//scroll content here according to height of keyboard ;
}
}
我是做错了什么,还是新版本有问题?我需要帮助。谢谢。我也经历过同样的事情,只是在iOS8中 我通过添加用于处理通知的信号量解决了这个问题:
@implementation MyViewController
//workaround to iOS 8 bug where the keyboard notification is fired up twice
static BOOL flag;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)note {
if (flag) {
return;
}
flag = YES;
//your code for handling keyboard display
}
- (void)ga_keyboardWillHide:(NSNotification *)note {
if (!flag) {
return;
}
flag = NO;
// your code for handling keyboard hiding
}
@end
有人找到了解决方案吗?您使用的是
UIScrollViewKeyboardDismissModeInteractive
?当您以交互方式关闭键盘时,UIKeyboardWillHideNotification
似乎被触发了两次:一次是在向下拖动键盘后抬起手指,第二次是在键盘最终脱离屏幕时。您可以通过在收到通知时在通知上记录userInfo
字典来验证这一点。我不确定这是苹果公司的故意行为,还是一个bug,但你可以使用dict中的帧信息来确定如何调整你的滚动视图,或者使用英雄的修复程序。