在我的应用程序中,我显示MFMailComposeViewController以显示默认邮件应用程序。如何关闭在控制器中打开的键盘?
答案 0 :(得分:1)
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:@selector(firstResponder)];
[firstResponder resignFirstResponder];
我希望这会帮助你.. :)
答案 1 :(得分:1)
在viewcontroller.h
添加:
@property (nonatomic) UITapGestureRecognizer *tapRecognizer;
现在在.m文件中,将其添加到ViewDidLoad函数:
- (void)viewDidLoad
{
[super viewDidLoad];
//Keyboard stuff
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapRecognizer];
}*
另外,在.m文件中添加此功能:
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}
答案 2 :(得分:0)
不幸的是,即使我们尝试使用 endEditing;
强制关闭键盘,该功能也不起作用。在我的设备上,它似乎不会被解雇。
以下应该是应该有效的答案:
完成后显示 MFMailComposeViewController
,然后在endEditing:
的视图中致电MFMailComposeViewController
。
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[self presentViewController:mailVC animated:YES completion:^{
[mailVC.view endEditing:YES];
}];
的更新:强>
不建议这样做但是,这是一种有效的hackish方式:
目标-C:
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[self presentViewController:mailVC animated:YES completion:^{
UITextField *a = [[UITextField alloc] init];
[mailVC.view addSubview:a];
[a becomeFirstResponder];
[mailVC.view endEditing:YES];
}];
夫特:
let mailVC = MFMessageComposeViewController()
self.presentViewController(mailVC, animated: true) { () -> Void in
let a = UITextField()
mailVC.view.addSubview(a)
a.becomeFirstResponder()
mailVC.view.endEditing(true)
}