我有几个案例,测试人员报告说,只要他们开始在我的应用程序的某些字段中输入键盘,键盘就会消失。我使用模拟器跟踪流程,并在手机上进行调试时,问题没有发生。然而,当我在一个不受限制的手机上尝试时,它发生得相当一致。
这是一些相关的代码。所有这些都是在用户点击文本字段外时隐藏键盘。我的UIViews是我的Touchview类的子类,它接收所有触摸:
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:
- (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中:
- (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如何响应命中的变化。
答案 0 :(得分:0)
问题是IOS 8.0(至少)改变了如何将命中发送到视图。以前,键盘窗口或文本字段中的命中被吸收而不会传递到hitview,这适用于此应用程序。 8.0改变了这一点,所有命中都会发送。
为了解决这个问题,我在命中测试代码中捕获了键盘窗口,并将hitView与textfield视图进行了比较以过滤掉这些:
Touchview.m现在有以下内容。 keyboardView方法改编自Stack Overflow答案:iOS: How to access the `UIKeyboard`?
-(UIView *) keyboardView {
//Return the current keyboard view
UIWindow * keyboardWindow;
UIView* keyboardView;
UIView* primaryKeyboardView;
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
if ([NSStringFromClass([window class]) isEqualToString:@"UITextEffectsWindow"])
{
keyboardWindow = window;
break;
}
}
for(int i = 0 ; i < [keyboardWindow.subviews count] ; i++)
{
keyboardView = [keyboardWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if([[keyboardView description] hasPrefix:@"<UIPeripheralHost"] == YES){
primaryKeyboardView = keyboardView;
}
//This code will work on iOS 8.0
else if([[keyboardView description] hasPrefix:@"<UIInputSetContainerView"] == YES){
for(int i = 0 ; i < [keyboardView.subviews count] ; i++)
{
UIView* hostkeyboard = [keyboardView.subviews objectAtIndex:i];
if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES){
primaryKeyboardView = hostkeyboard;
}
}
}
}
return primaryKeyboardView;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView * touchedView = [super hitTest:point withEvent:event];
NSSet* touches = [event allTouches];
UIView * keyboardView = [self keyboardView];
if (touchedView != keyboardView) { //Ignore the hit if it's a keyboard
[self.touchDelegate handleTouches:touches withEvent:event inView:touchedView];
}
return touchedView;
}
新的委托方法:
-(UIView *) handleTouches:(NSSet *)touches withEvent:(UIEvent *)event inView:(UIView *) hitView {
if (![hitView isKindOfClass:[UIResponder class]]) {
[[UIResponder firstResponder] resignFirstResponder];
}
return hitView;
}
firstResponder方法来自这个要点:https://gist.github.com/vilanovi/e52face5c6f00ce5254d
答案 1 :(得分:0)
已在iOS 8.0.2中修复,不再需要。
答案 2 :(得分:0)
对于我来说,这仍然发生在8.02中
答案 3 :(得分:0)
我发现同样的问题 - 当用户试图键入键盘时,键盘会被解雇。
我的解决方案是一种解决方法。为了解决这个问题,我在键盘下面创建了一个虚拟的空UIView。此虚拟视图吸收并消耗敲击事件,键盘不会被解除。我在&#34; UIKeyboardDidShowNotification&#34;上创建了视图。并在&#34; UIKeyboardWillHideNotification&#34;上删除它。这解决了问题,并且适用于屏幕方向更改。
以下是我的视图控制器中的代码,其中包含显示键盘的文本字段:
/** Dummy view placed underneath the keyboard to prevent its dismissal. */
@property(nonatomic, strong) UIView *dummyView;
在viewDidLoad
方法中,我们注册了键盘显示/隐藏通知:
// 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];
然后,在收到显示的&#39;键盘后,通知我们在键盘下面创建虚拟视图:
- (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];
}
...并在隐藏键盘时将其删除:
- (void)keyboardWillBeHidden:(NSNotification*)notification {
[self.dummyView removeFromSuperview];
self.dummyView = nil;
}