Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MFMailComposeViewController Keyboard Issue

How do i dismiss the keyboard without pressing the Send or Cancel button in MFMailComposeViewController?!

Thanks for any help.

like image 989
Mobile Developer iOS Android Avatar asked Feb 02 '11 09:02

Mobile Developer iOS Android


3 Answers

Can you try this.

UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:@selector(firstResponder)];
[firstResponder resignFirstResponder];

hope this helps....

like image 137
visakh7 Avatar answered Nov 10 '22 22:11

visakh7


I experienced a similar problem: For some reason iOS does not dismiss the Keyboard of a MFMailComposeViewController when the application enters background (the dismiss happens when the application becomes active again). However iOS dismisses the keyboard if the first responder is a simple element (e.g. textview). Calling resignFirstResponder did not work for me in this particular case. Because I switch windows on applicationBecomeActive (to show a login screen) I ended up having multiple keyboards above each other (the one on the top not working). I found a simple workaround to dismiss the keyboard of an MFMailComposeViewController when the application resigns active:

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Workaround: MFMailComposeViewController does not dismiss keyboard when application enters background
    UITextView *dummyTextView = [[UITextView alloc] init];
    [self.window.rootViewController.presentedViewController.view addSubview:dummyTextView];
    [dummyTextView becomeFirstResponder];
    [dummyTextView resignFirstResponder];
    [dummyTextView removeFromSuperview];
    // End of workaround
}

This will implicitly resign the first responder if we have any viewController that is currently beeing presented.

like image 41
Lukas Gross Avatar answered Nov 11 '22 00:11

Lukas Gross


While you probably can do it by finding whichever view is the first responder and calling resignFirstResponder on it (unless you're on iPad and MFMailComposeViewController uses UIModalPresentationFormSheet), Apple might reject your app for it. Quoth the documentation:

Important: The mail composition interface itself is not customizable and must not be modified by your application.

This could easily be construed to include the behavior of the keyboard.

like image 2
Anomie Avatar answered Nov 11 '22 00:11

Anomie