Goutham
Goutham

Reputation: 297

how to dismiss the key board in mfmailcomposerview

In my iphone app. in MFMailComposerView view when i am clicking to recepients then key board appears . After i am clicking return key in the key board. but key board not disappear.

Upvotes: 1

Views: 334

Answers (2)

Midhun MP
Midhun MP

Reputation: 107211

You can use the following code for this.

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

But if you use this in your app, Apple will surely reject your app because UIWindow's firstResponder method is a private API.

Reference : SO

Upvotes: 0

Paras Joshi
Paras Joshi

Reputation: 20541

Use UIWindow notifications keyboard and just use bellow code for display the MFMailComposerViewController ..

- (IBAction)showMailController {
    //Present mail controller on press of a button, set up notification of keyboard showing and hiding
    [nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
    [nc addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];           
    //... and so on
}        
- (void)keyboardWillShow:(NSNotification *)note {
    //Get view that's controlling the keyboard
    UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView* firstResponder = [keyWindow performSelector:@selector(firstResponder)];

    //set up dimensions of  dismiss keyboard button and animate it into view, parameters are based on landscape orientation, the keyboard's dimensions and this button's specific dimensions
    CGRect t;
    [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
    button.frame = CGRectMake(324,(290-t.size.height),156,37);
    button.alpha = 0.0;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0];    
    [[[[[firstResponder superview] superview] superview] superview] addSubview:button];
    button.alpha = 1.0;
    button.frame = CGRectMake(324,(253-t.size.height),156,37);
    [UIView commitAnimations];
}

- (IBAction)dismissKeyboardInMailView {
    //this is what gets called when the dismiss keyboard button is pressed
    UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView* firstResponder = [keyWindow performSelector:@selector(firstResponder)];
    [firstResponder resignFirstResponder];
}

- (void)keyboardWillHide:(NSNotification *)note {
    //hide button here
    [button removeFromSuperview];
}

i got some of this code from this link..

programmatically-align-a-toolbar-on-top-of-the-iphone-keyboard

Upvotes: 3

Related Questions