如何在mfmailcomposer视图中关闭键盘

时间:2013-01-04 09:21:30

标签: iphone objective-c ios

在我的iPhone应用程序中。在MFMailComposerView视图中,当我点击接收者时,会出现键盘。我点击键盘上的返回键后。但是关键板并没有消失。

2 个答案:

答案 0 :(得分:3)

使用UIWindow通知键盘,只需使用以下代码显示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];
}

我从这个链接获得了一些代码..

  

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

答案 1 :(得分:0)

您可以使用以下代码。

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

但是如果您在自己的应用中使用此功能,Apple肯定会拒绝您的应用,因为UIWindow的firstResponder方法是私有API。

参考:SO