在iOS 7上,VIEWDID消失后键盘不会消失
在我们的应用程序中,存在这样一种情况:用户在文本框中输入一些内容,然后按下后退按钮返回主屏幕 如果我们在iOS 7上运行,键盘不会消失,它只是停留在那里。用户仍然可以在应用程序中导航,但所有文本字段都被禁用,这意味着您不能在任何地方输入文本。用户唯一的选择是关闭应用程序并重新启动 我们试图添加在iOS 7上,VIEWDID消失后键盘不会消失,ios,ios7,uikeyboard,Ios,Ios7,Uikeyboard,在我们的应用程序中,存在这样一种情况:用户在文本框中输入一些内容,然后按下后退按钮返回主屏幕 如果我们在iOS 7上运行,键盘不会消失,它只是停留在那里。用户仍然可以在应用程序中导航,但所有文本字段都被禁用,这意味着您不能在任何地方输入文本。用户唯一的选择是关闭应用程序并重新启动 我们试图添加resignFirstResponder消息,但没有任何帮助 涉及到很多代码,我们正在积极处理这个问题。同时,有没有人也遇到过这个问题,并且可能找到了解决问题的方法?尝试[self.view resignF
resignFirstResponder
消息,但没有任何帮助
涉及到很多代码,我们正在积极处理这个问题。同时,有没有人也遇到过这个问题,并且可能找到了解决问题的方法?尝试
[self.view resignFirstResponder]
,而不是[textfield resignFirstResponder]
在viewwill上消失。[textfield resignFirstResponder]应该做这项工作,但为了确保不在所有文本字段中循环,您可以使用:
[self.view endEditing:YES];
从文档:
用于使作为第一响应者的视图或任何子视图退出
(可选强制)
我在为iOS 7编译应用程序时遇到了与您相同的问题,我做了以下更改:
[textfield resignFirstResponder]
,例如:
[_passwordInput resignFirstResponder];
[_emailInput resignFirstResponder];
[self performSegueWithIdentifier:@"forgotPassword" sender:self];
视图中添加[textfield resignFirstResponder]
,例如:
- (void) viewWillDisappear:(BOOL)animated
{
[_passwordInput resignFirstResponder];
[_emailInput resignFirstResponder];
}
UIModalPresentationFormSheet
显示的,请将其添加到viewController中,以确保文本字段将响应resignFirstResponder
:
- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}
viewwilldiscover
检查用户何时按下后退按钮,然后在[super viewwilldiscover]
之前调用resignFirstResponder
,如下所示:
-(void) viewWillDisappear:(BOOL)animated
{
[_passwordInput resignFirstResponder];
[_emailInput resignFirstResponder];
[super viewWillDisappear:animated];
}
总的来说,我觉得这很有用
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
您可以在viewwilldemouse:
或viewdidemouse:
这将隐藏键盘而不引用当前聚焦的文本字段我只有在
UITabBarController
(iOS 8.3
)中的MoreViewController
有同样的问题。也许这个解决方案不是很“好”,也有点复杂,但似乎它是可行的,希望它也能帮助你
@interface ViewController ()
@property (nonatomic) BOOL needToHideKeyboard;
@property (nonatomic, strong) IBOutlet UITextField *txtField;
@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.needToHideKeyboard = NO;
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[self hideKeayboard];
}
- (void)hideKeayboard
{
if (self.needToHideKeyboard) {
[self.txtField resignFirstResponder];
}
}
- (void)keyboardWasShown:(NSNotification *)notification
{
self.needToHideKeyboard = YES;
NSDictionary *info = [notification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// Shift scroll view content insets on the keyboard height
UIEdgeInsets contentInsets = self.scrollView.contentInset;
contentInsets.bottom = keyboardSize.height;
self.scrollView.contentInset = contentInsets;
}
- (void)keyboardWillBeHidden:(NSNotification *)notification
{
self.needToHideKeyboard = NO;
// Reset keyboard content insets
UIEdgeInsets contentInsets = self.scrollView.contentInset;
contentInsets.bottom = [self.bottomLayoutGuide length];
self.scrollView.contentInset = contentInsets;
}
@end
如果视图控制器实现了
textfielddidediting
,请确保在视图消失时不要将另一个视图设置为第一响应者<当您调用辞职第一响应者
或[self.view endEditing:YES]
时,将调用code>textfielddidediting
[self.view endEditing:YES];
停止使用我的设备iOS9.x
我们也可以在视图中执行此操作
for (UIView *subview in self.view.subviews) {
if ([subview canPerformAction:@selector(endEditing:) withSender:nil]) {
[subview endEditing:YES];
}
if ([subview canResignFirstResponder]) {
[subview resignFirstResponder];
}
}
这将在响应者之间循环并退出响应者状态。对我不起作用,似乎在调用viewwilldefine
之前,第一响应者已被清除,只是专门告诉文本字段退出似乎有效。对于为什么在iOS7中会发生这种情况感到困惑。在iOS 8.0中似乎出现了问题-如果您使用搜索栏重新进入屏幕,键盘将重新出现。当我重新进入屏幕时,键盘再次出现。iOS 8.2