【问题标题】:How to detect keyboard type changes with the size, type, suggestion bar height of new keyboard type?如何检测键盘类型随新键盘类型的大小、类型、建议栏高度的变化?
【发布时间】:2016-05-27 07:56:14
【问题描述】:

有什么方法可以检测键盘类型随大小、类型和建议栏高度的变化,比如从英文键盘变为包含某种建议栏的印地语(见截图)。

普通英文键盘

第一个问题

更改为印地语 LIPI 后 - 当我更改时,一切都很好,因为英文和印地语键盘的大小相同,但在开始输入印地语 Lipi 后建议覆盖 TextField

第二个问题

改成 Emoji 后 - Emoji 键盘比英文高一点,所以键盘再次覆盖TextField

【问题讨论】:

  • textfield.autocorrectionType = UITextAutocorrectionTypeNo;
  • 我已经在使用UITextAutocorrectionTypeNo,但它只隐藏了对英语而不是印地语 Lipi 的建议。

标签: ios objective-c uikeyboard custom-keyboard


【解决方案1】:

为名为“UIKeyboardWillChangeFrameNotification”的通知添加观察者。

通知的“userInfo”字典在屏幕上有多个键盘框架。

添加观察者

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];

带有记录器的选择器

- (void)logNotification:(NSNotification *)notification {

    NSLog(@"%@", notification);
}

userInfo 字典内容

userInfo = {
    UIKeyboardAnimationCurveUserInfoKey = 7;
    UIKeyboardAnimationDurationUserInfoKey = 0;
    UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
    UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 441.5}";
    UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}";
    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 315}, {320, 253}}";
    UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";
    UIKeyboardIsLocalUserInfoKey = 1;
}}

【讨论】:

    【解决方案2】:

    我也面临同样的问题。我通过使用下面的代码解决了

    CGSize keyboardSize = [aNotification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    

    使用 UIKeyboardFrameEndUserInfoKey 而不是使用 UIKeyboardFrameBeginUserInfoKey。

    【讨论】:

      【解决方案3】:

      斯威夫特 4

      注册观察员

      NotificationCenter.default.addObserver(self,selector:#selector(KeyboardWillChangeFrame),name:NSNotification.Name.UIKeyboardWillChangeFrame,object: nil)
      

      功能

          @objc func KeyboardWillChangeFrame(_ notification: Notification){
          if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
              let keyboardHeight = keyboardSize.height
              print("New Keyboard Height:",keyboardHeight)
           }
      }
      

      【讨论】:

      • 我应该用这个代替 UIKeyboardWillShowNotification 并隐藏通知吗?
      • 不,您应该将它与显示和隐藏通知一起使用才能处理所有事情
      【解决方案4】:

      您需要根据屏幕上的显示来获取键盘的高度

      • UIKeyboardWillShowNotification 发送时键盘高度为 224

      根据UIKeyboardWillShowNotificationUIKeyboardWillHideNotification 通知注册观察员:

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

      为键盘高度设置一个全局变量:

      CGFloat _currentKeyboardHeight = 0.0f;
      

      实现 keyboardWillShowkeyboardWillHide 以响应当前键盘高度的变化:

      - (void)keyboardWillShow:(NSNotification*)notification {
         NSDictionary *info = [notification userInfo];
         CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
         CGFloat deltaHeight = kbSize.height - _currentKeyboardHeight; 
         // Write code to adjust views accordingly using deltaHeight
         _currentKeyboardHeight = kbSize.height;
      }
      
      - (void)keyboardWillHide:(NSNotification*)notification {
         NSDictionary *info = [notification userInfo];
         CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
         // Write code to adjust views accordingly using kbSize.height
         _currentKeyboardHeight = 0.0f;
      }
      

      【讨论】:

      • 请仔细阅读问题。我询问何时更改键盘类型而不是键盘出现或消失通知。因为像表情符号这样的键盘有不同的大小。当我们更改键盘类型时,UIKeyboardWillShowNotificationUIKeyboardWillHideNotification 不会触发通知。
      猜你喜欢
      • 1970-01-01
      • 2010-11-10
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 2013-07-31
      • 1970-01-01
      • 2014-08-06
      相关资源
      最近更新 更多