使用返回键关闭iPhone Mail View控制器键盘

时间:2010-02-04 23:10:45

标签: iphone keyboard

当用户点击返回键时,如何关闭MFMailComposeViewController中的键盘?

2 个答案:

答案 0 :(得分:0)

无法改变MFMailComposeViewController的行为(编辑:我的意思是仅使用公共API,以免Apple拒绝您的应用程序 - 很可能是如果您正在构建内部应用程序,则“非法”执行此操作。

http://developer.apple.com/iphone/library/documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html

引用:“重要提示:邮件撰写界面本身不可自定义,不得由您的应用程序修改。”

与Apple的HIG一致,始终为发送邮件提供单一,一致的行为。

答案 1 :(得分:0)

我相信我刚刚开发的以下解决方案不会修改或损坏邮件视图控制器中的可靠用户体验 - 它只是为那些想要解除键盘的人增强了它。我不改变现有元素的功能,我只添加一个新元素。在显示键盘后获得键盘高度的代码部分来自this answer。这是:

- (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];
}