【问题标题】:Dismiss keyboard on rotate旋转时关闭键盘
【发布时间】:2014-09-09 13:56:26
【问题描述】:

我正在考虑添加一个观察者来监听方向更改通知。我想要做的是关闭键盘,如果它当前处于启动状态。

我无法找到不涉及找到具有焦点的单个控件的示例。

【问题讨论】:

  • 有什么问题...您的通知有效吗?您所说的有重点的个人控制是什么意思?
  • 是的,我收到了通知。但是到那时,如果键盘存在,我如何“全局”关闭它?
  • 您在这里得到了很多答案,但冒着听起来傲慢的风险,我认为我的答案正是您在这种情况下所要寻找的。我建议先检查一下。

标签: c# ios xamarin.ios xamarin


【解决方案1】:

假设您的编辑字段是 self.view 的子视图,您可以关闭键盘而不管它的响应者使用:

[self.view endEditing:YES];

【讨论】:

  • @CrimsonChris 为了清楚起见,我将添加它。
  • 谢谢,这正是我想要的。
【解决方案2】:

只需从您的 UITextField 对象调用 resignFirstResponder,例如:

[textField resignFirstResponder];

【讨论】:

    【解决方案3】:

    您是否考虑过以下UIViewController Class Reference

    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {  
        // dismiss keyboard (if it was presented via a textField, etc.
        [self.myTextField resignFirstResponder];
    
        // or
        // [self.view endEditing:YES]; 
    
        // you can check for specific orientation via UIInterfaceOrientation - for example
        // landscape
        if (toInterfaceOrientation != UIInterfaceOrientationPortrait && toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown) {
            // do something
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 2015-03-17
      • 2020-01-03
      • 1970-01-01
      • 2017-03-25
      • 2013-01-08
      相关资源
      最近更新 更多