【问题标题】:how to dismiss the key board in mfmailcomposerview如何在 mfmailcomposer 视图中关闭键盘
【发布时间】:2012-12-18 17:21:47
【问题描述】:

在我的 iPhone 应用程序中。在MFMailComposerView 视图中,当我单击收件人时,会出现键盘。在我单击键盘中的返回键后。但键盘没有消失。

【问题讨论】:

  • 不,键盘永远无法关闭
  • 通过一些递归响应者链黑客攻击,这可能在 iOS 6 之前有效,但现在邮件控制器是远程的,我绝对不建议您尝试远程辞去它的第一响应者。
  • 我认为返回键从一个文本字段移动到 MFMailComposerView 中的另一个文本字段。当最后一个文本字段返回时。
  • 不要将 Xcode 标签用于与 Xcode 无关的问题!

标签: iphone objective-c ios


【解决方案1】:

使用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

【讨论】:

【解决方案2】:

您可以使用以下代码。

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

但是如果你在你的应用中使用这个,Apple 肯定会拒绝你的应用,因为 UIWindow 的 firstResponder 方法是一个私有 API。

参考:SO

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多