Reputation: 281
In my app I display the MFMailComposeViewController to display default mail app. How to dismiss the keyboard which opens up in the controller?
Upvotes: 1
Views: 263
Reputation:
In your viewcontroller.h
add:
@property (nonatomic) UITapGestureRecognizer *tapRecognizer;
Now in the .m file, add this to your ViewDidLoad function:
- (void)viewDidLoad
{
[super viewDidLoad];
//Keyboard stuff
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapRecognizer];
}*
Also, add this function in the .m file:
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}
Upvotes: 1
Reputation: 124
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:@selector(firstResponder)];
[firstResponder resignFirstResponder];
I hope This Will Help You.. :)
Upvotes: 1
Reputation: 5990
Unfortunately it look's like even if we try to force close the keyboard using endEditing;
that function takes no effect. On my device it doesn't seem like the keyboard can be dismissed.
Below would have been the answer that should have worked:
Display the MFMailComposeViewController
with a completion and call endEditing:
on the MFMailComposeViewController
's view.
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[self presentViewController:mailVC animated:YES completion:^{
[mailVC.view endEditing:YES];
}];
UPDATE:
Wouldn't suggest doing this but, here is a hackish way that works:
Objective-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];
}];
Swift:
let mailVC = MFMessageComposeViewController()
self.presentViewController(mailVC, animated: true) { () -> Void in
let a = UITextField()
mailVC.view.addSubview(a)
a.becomeFirstResponder()
mailVC.view.endEditing(true)
}
Upvotes: 1