使用IOS 8编辑时,键盘会间歇性地消失 [英] Keyboard intermittently disappears when editing using IOS 8

查看:108
本文介绍了使用IOS 8编辑时,键盘会间歇性地消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个案例,测试人员报告说,只要他们开始在我的应用程序的某些字段中输入键盘,键盘就会消失。我使用模拟器跟踪流程,并在手机上进行调试时,问题没有发生。然而,当我在一个不受限制的手机上尝试它时,它发生得相当一致。

I had several cases where testers reported that the keyboard would disappear whenever they started typing in some fields in my app. I traced the flow using the simulator and while debugging on a phone and the problem didn't occur, ever. However, when I tried it on an untethered phone it happened fairly consistently.

这是一些相关的代码。所有这些都是在用户点击文本字段外时隐藏键盘。我的UIViews是我的Touchview类的子类,它接收所有触摸:

Here's some pertinent code. All of this is to hide the keyboard when a user taps outside a textfield. My UIViews are subclasses of my Touchview class, which receives all touches:

TouchView.h:

TouchView.h:

@protocol TouchViewDelegate <NSObject>

-(UIView *) handleTouches:(NSSet *)touches withEvent:(UIEvent *)event inView:(UIView *) view;

@end

@interface TouchView : UIScrollView

@property (nonatomic, strong) id <TouchViewDelegate> touchDelegate;

@end

TouchView.m:

TouchView.m:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView * touchedView = [super hitTest:point withEvent:event];
    NSSet* touches = [event allTouches];
    [self.touchDelegate handleTouches:touches withEvent:event inView:touchedView];
    return touchedView;

}

我配置了主视图作为Touchview并将其包含在viewDidLoad中:

I configured the main view as a Touchview and included this in the viewDidLoad:

- (void)viewDidLoad
{
[super viewDidLoad];

HMWTouchView * touchView = (HMWTouchView*) self.view;

touchView.touchDelegate = self;
...
}

这是委托方法的实现:

-(UIView *) handleTouches:(NSSet *)touches withEvent:(UIEvent *)event inView:(UIView *) hitView {

if (![hitView isKindOfClass:[UIButton class]]) {
    [[UIResponder firstResponder] resignFirstResponder];
}
return self.view;
}

这看起来至少是IOS 8如何响应点击率的变化。

This looks like it is at least a change in how IOS 8 responds to hits.

推荐答案

我发现了同样的问题 - 当用户试图键入键盘时键盘被解除。

I observed the same problem - the keyboard gets dismissed when the user tries to type on it.

我的解决方案是一种解决方法。为了解决这个问题,我在键盘下面创建了一个虚拟的空UIView。此虚拟视图吸收并消耗敲击事件,键盘不会被解除。我在UIKeyboardDidShowNotification上创建视图,并在UIKeyboardWillHideNotification上将其删除。这解决了问题,并且适用于屏幕方向更改。

My solution is a workaround. To solve the problem I create a dummy empty UIView underneath the keyboard. This dummy view absorbs and consumes tap events and the keyboard is not dismissed. I create the view on the "UIKeyboardDidShowNotification" and remove it on "UIKeyboardWillHideNotification". This gets rid of the problem and works well with screen orientation changes.

以下是我的视图控制器中的代码,其中包含显示键盘的文本字段:

Here is the code in my view controller that contains the text fields that show up the keyboard:

/** Dummy view placed underneath the keyboard to prevent its dismissal. */
@property(nonatomic, strong) UIView *dummyView;

viewDidLoad 方法中,我们注册了键盘显示/隐藏通知:

In the viewDidLoad method we register for keyboard show/hide notifications:

// keyboard notifications - we need this to prevent keyboard dismissal
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil];

然后,在收到'键盘显示'通知后,我们在键盘下方创建虚拟视图:

Then, after receiving the 'keyboard shown' notification we create the dummy view underneath the keyboard:

- (void)keyboardWasShown:(NSNotification*)notification {
    // we need this to prevent keyboard dismissal    
    NSDictionary* info = [notification userInfo];
    CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrame = [self.view convertRect:keyboardFrame fromView:nil];
    self.dummyView = [[UIView alloc] initWithFrame:keyboardFrame];
    [self.view addSubview:self.dummyView];
}

...并在隐藏键盘时删除它:

... and remove it when the keyboard is hidden:

- (void)keyboardWillBeHidden:(NSNotification*)notification {
    [self.dummyView removeFromSuperview];
    self.dummyView = nil;
}

这篇关于使用IOS 8编辑时,键盘会间歇性地消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆