ios 模态对话框不会关闭键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3019709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Modal Dialog Does Not Dismiss Keyboard
提问by DenVog
I am running into an issue where the keyboard does not get dismissed when leaving a UITextField
or UITextView
in a UIModalPresentationFormSheet
. In addition, I've created a large button to serve as the view's background so if the user taps outside the fields it gets triggered. I am using the same code in a regular view controller, and it works as expected. In the modal view controller it does nothing. Any suggestions would be appreciated.
我遇到了一个问题,即离开时,键盘不会被解雇UITextField
或UITextView
在UIModalPresentationFormSheet
。此外,我创建了一个大按钮作为视图的背景,因此如果用户在字段外点击它就会被触发。我在常规视图控制器中使用相同的代码,它按预期工作。在模态视图控制器中它什么都不做。任何建议,将不胜感激。
- (BOOL)textFieldShouldReturn:(id)sender {
[titleTextField resignFirstResponder];
return YES;
}
- (BOOL)textViewShouldReturn:(id)sender {
[synopsisTextView resignFirstResponder];
return YES;
}
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)textViewDoneEditing:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)backgroundClick:(id)sender {
[titleTextField resignFirstResponder];
[synopsisTextView resignFirstResponder];
}
回答by aslisabanci
Overriding disablesAutomaticKeyboardDismissal
to return NO as below fixed the same problem of mine. You should put this code to your view controller, from which you initiate the keyboard:
如下覆盖disablesAutomaticKeyboardDismissal
返回 NO 解决了我的相同问题。您应该将此代码放在视图控制器中,从中您可以启动键盘:
- (BOOL)disablesAutomaticKeyboardDismissal {
return NO;
}
Also, check this SOquestion if you want to get a detailed explanation.
另外,如果您想获得详细说明,请检查此SO问题。
回答by Chris Trahey
For those having trouble with UINavigationController
, I think there is a better solution than a category on UIViewController
. We should change the behavior of UINavigationController
to ask its topViewController
(in my opinion, this is how all ViewController
containers should handle this).
对于那些遇到问题的人UINavigationController
,我认为有一个比分类更好的解决方案UIViewController
。我们应该改变UINavigationController
询问它的行为topViewController
(在我看来,这是所有ViewController
容器应该如何处理这个)。
@implementation UINavigationController (DelegateAutomaticDismissKeyboard)
- (BOOL)disablesAutomaticKeyboardDismissal {
return [self.topViewController disablesAutomaticKeyboardDismissal];
}
回答by Kalle
If you're presenting a modal view with presentation style "form sheet", Apple apparently does not dismiss the keyboard, thinking that they don't want the keyboard to jump in and out where a user will be doing a lot of editing (i.e. "forms"). The fix would be to change presentation style or live with it.
如果您使用演示样式“表单”呈现模态视图,Apple 显然不会关闭键盘,认为他们不希望键盘跳入和跳出用户将进行大量编辑的位置(即“形式”)。解决方法是改变演示风格或接受它。
回答by SafeFastExpressive
If you implement the UITextFieldDelegate protocol you can inadvertently cause this behavior if you do text validation. If your validation codes returns false from textFieldShouldEndEditing when the text is invalid, the field can't relinquish it's firstResponder status and the keyboard will remain on screen in the next view.
如果您实现了 UITextFieldDelegate 协议,则在进行文本验证时可能会无意中导致此行为。如果您的验证代码在文本无效时从 textFieldShouldEndEditing 返回 false,则该字段无法放弃其 firstResponder 状态,并且键盘将在下一个视图中保留在屏幕上。
More details at UITextField's keyboard won't dismiss. No, really
回答by azdev
回答by Mike Gledhill
The disablesAutomaticKeyboardDismissal
refused to work for me on iOS 7.
在disablesAutomaticKeyboardDismissal
iOS上的7不肯为我工作。
But... I managed to solve this issue by simply disablingthe UITextFields on the screen.
但是...我通过简单地禁用屏幕上的 UITextFields设法解决了这个问题。
My solution is described here.
我的解决方案在这里描述。
This workaround even works on Modal UIViewController
s.
这种解决方法甚至适用于 Modal UIViewController
s。
Yeah... it surprised me aswell !!
是啊……我也很惊讶!!