代码之家  ›  专栏  ›  技术社区  ›  Kalle

如果模态ViewController呈现样式为UIModalPresentationFormSheet,iPad键盘将不会关闭

  •  212
  • Kalle  · 技术社区  · 16 年前

    有关iOS 4.3的解决方案,请参阅公认答案(而非最热门答案)。

    问题 是关于在iPad键盘上发现的一种行为,如果在与导航控制器的模式对话框中显示,它将拒绝被忽略。

    navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
    

    键盘不肯拆。如果我把这一行注释掉,键盘就会消失得很好。

    ...

    我有两个文本字段,用户名和密码;username有一个Next按钮,password有一个Done按钮。如果我在模态导航控制器中演示,键盘不会消失。

    broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
    [self.view addSubview:b.view];
    

    不起作用

    broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
    UINavigationController *navigationController = 
    [[UINavigationController alloc]
     initWithRootViewController:b];
    navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
    navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:navigationController animated:YES];
    [navigationController release];
    [b release];
    

    broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
    b.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:b animated:YES];
    [b release];
    

    broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
    UINavigationController *navigationController = 
        [[UINavigationController alloc]
             initWithRootViewController:b];
    [self presentModalViewController:navigationController animated:YES];
    [navigationController release];
    [b release];
    
    13 回复  |  直到 12 年前
        1
  •  116
  •   Greg Brown    7 年前

    disablesAutomaticKeyboardDismissal 返回 NO :

    - (BOOL)disablesAutomaticKeyboardDismissal {
        return NO;
    }
    
        2
  •  172
  •   Mike Weller    15 年前

    这被苹果的工程师们归类为“按计划工作”。我前一阵子给它装了个窃听器。他们的理由是,用户通常将以模态形式输入数据,因此他们试图“提供帮助”,并使键盘保持可见,而模态视图中的各种转换通常会导致键盘重复显示/隐藏。

    here is the response 一位苹果工程师在开发者论坛上的发言:

    您的视图是否以UIModalPresentationFormSheet样式呈现?为了避免频繁的进出动画,键盘有时会保持在屏幕上,即使没有第一反应。这不是虫子。

    这给很多人带来了问题(包括我自己),但目前似乎没有办法解决。

    在iOS 4.3及更高版本中,现在可以在视图控制器上实现“-disablesAutomatickeyboarddisassal”,以返回NO:

    - (BOOL)disablesAutomaticKeyboardDismissal {
        return NO;
    }
    

    这解决了这个问题。

        3
  •  150
  •   MrHus    15 年前

    UINavigationController . 然后你必须设置 disablesAutomaticKeyboardDismissal 在导航控制器上,而不是在视图控制器上。你可以很容易地用分类来做这件事。

    #import <Foundation/Foundation.h>
    
    @interface UINavigationController (KeyboardDismiss)
    
    - (BOOL)disablesAutomaticKeyboardDismissal;
    
    @end
    

    文件:UINavigationController+keyboarddismission.m

    #import "UINavigationController+KeyboardDismiss.h"
    
    @implementation UINavigationController(KeyboardDismiss)
    
    - (BOOL)disablesAutomaticKeyboardDismissal
    {
        return NO;
    }
    
    @end
    

    导航控制器。

        4
  •  51
  •   Dharmesh Dhorajiya    10 年前

    我通过使用 UIModalPresentationPageSheet

    viewController.modalPresentationStyle = UIModalPresentationPageSheet;
    viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:viewController animated:YES];
    viewController.view.superview.autoresizingMask = 
        UIViewAutoresizingFlexibleTopMargin | 
        UIViewAutoresizingFlexibleBottomMargin;    
    viewController.view.superview.frame = CGRectMake(
        viewController.view.superview.frame.origin.x,
        viewController.view.superview.frame.origin.y,
        540.0f,
        529.0f
    );
    viewController.view.superview.center = self.view.center;
    [viewController release];
    
        5
  •  1
  •   Adam    15 年前

    如果你切换一个不同的模式显示,你可以让键盘消失。它不漂亮,也没有动画下来,但你可以让它消失。

    UIViewController 当你不想再使用键盘的时候,可以叫它:

    @interface _TempUIVC : UIViewController
    @end
    
    @implementation _TempUIVC
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
    }
    @end
    
    @implementation UIViewController (Helpers)
    
    - (void)_dismissModalViewController {
        [self dismissModalViewControllerAnimated:NO];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
        [self release];
    }
    
    - (void)forceKeyboardDismissUsingModalToggle:(BOOL)animated {
        [self retain];
        _TempUIVC *tuivc = [[_TempUIVC alloc] init];
        tuivc.modalPresentationStyle = UIModalPresentationCurrentContext;
        [self presentModalViewController:tuivc animated:animated];
        if (animated) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_dismissModalViewController) name:UIKeyboardDidHideNotification object:nil];
        } else
            [self _dismissModalViewController];
        [tuivc release];
    }
    
    @end
    

    -亚当

        6
  •  1
  •   Maciej Swic    14 年前

    你也可以在一个通用应用程序中解决这个问题,只需检查习惯用法,如果是iPad,就不要自动弹出键盘,让用户点击任何他们想编辑的内容。

    可能不是最好的解决方案,但它非常简单,不需要任何花哨的黑客攻击,这将打破下一个主要的iOS版本:)

        7
  •  1
  •   Story    12 年前

    Class UIKeyboardImpl = NSClassFromString(@"UIKeyboardImpl");
    id activeInstance = [UIKeyboardImpl performSelector:@selector(activeInstance)];
    [activeInstance performSelector:@selector(dismissKeyboard)];
    
        8
  •  1
  •   Mike Gledhill    11 年前

    我发现了 disablesAutomaticKeyboardDismissal 添加一个 功能对我来说不起作用 UITextField

    屏幕上的键盘不会消失。

    使残废

    输入框 扔掉键盘。

        9
  •  0
  •   Neal L    16 年前

        10
  •  0
  •   mvds    16 年前

    也许 吧 不返回“否”,但返回“是”。所以它可以消失。

    textFieldShouldEndEditing 也回来了是吗?

    你开枪了吗 [nextResponder becomeFirstResponder] ?!

    我也有一些UITextView 属性设置为FALSE。

    我们能不能假设他们中没有一个 tag 的价值 secondField.tag+1

        11
  •  0
  •   Community Mohan Dere    9 年前

    对于那些在UINavigationController方面有问题的人,请参见我对类似问题的回答: https://stackoverflow.com/a/10507689/321785

    编辑:

        12
  •  0
  •   Tanuj Jagoori    11 年前

    可能不是一个完美的解决方案,但有效
    [自我观点]diting:YES];

        13
  •  0
  •   fivewood    8 年前
    Swift 4.1:
    extension UINavigationController {
       override open var disablesAutomaticKeyboardDismissal: Bool {
          return false
       }
    }