【问题标题】:SDK safe way to hide iPad keyboard when textview is in focus当 textview 处于焦点时,SDK 安全的方法来隐藏 iPad 键盘
【发布时间】:2012-05-25 20:08:40
【问题描述】:

我有一些 iPad 应用程序,用户可以在其中使用触摸屏或蓝牙键盘进行导航。 我有一些隐藏的 textView 处于焦点位置(第一响应者),在这里我检测到从键盘输入的内容。

但是,当我断开键盘时,我遇到了问题,出现了虚拟键盘。

我可以检查蓝牙键盘是否连接,并在 viewDidLoad 中设置或退出第一响应者吗?

我有通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];

当keyboardWillAppear被触发时,我可以以某种方式隐藏键盘吗? 我试过 [textView resignFirstResponder],但没有成功:|

【问题讨论】:

    标签: objective-c ios ipad uiview


    【解决方案1】:

    您可以将 inputView 设置为透明视图:

    UIView *emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    emptyView.backgroundColor = [UIColor clearColor];
    textView.inputView = emptyView;
    

    理论上,这会将屏幕键盘设置为空视图,使其不可见。如果它不接受没有框架的视图,则尝试将宽度和高度设置为 1。它不会影响外部键盘;它只是不会显示在设备上。

    【讨论】:

      【解决方案2】:

      您可以为此使用 performSelector:。

      - (void)hideKeyboard:(UITextView *)textView {
          [textView resignFirstResponder];
      }
      
      - (void)keyboardWillAppear:(NSNotification *)notification { 
          UITextView *textView = (UITextView *)[self.view viewWithTag:TEXTVIEW_TAG];
      
          [self performSelector:@selector(hideKeyboard:) withObject:textView];
      }
      
      - (void)viewDidLoad {
          [super viewDidLoad];
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
      }
      

      【讨论】:

      • 我也一样。还有一个更正,它是 TextView,而不是 TextField。
      【解决方案3】:

      您必须安排 textView 的第一响应者辞职到调度队列,因为成为第一响应者的过程可能还没有完成。使用 XCode 的调度模板的简单解决方案:

      int64_t delayInSeconds = 0;
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
          [self.textView resignFirstResponder];
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-20
        • 2013-07-16
        相关资源
        最近更新 更多