代码之家  ›  专栏  ›  技术社区  ›  Jeremy H

iOS 7键盘预测

  •  1
  • Jeremy H  · 技术社区  · 10 年前

    我们正在为iOS 7应用程序添加支持,以允许亚洲用户使用。然而,对于使用中文字符的键盘,if顶部有一个包含预测文本的滚动视图。一旦用户开始键入,水平滚动视图就会出现在键盘上方,并且永远不会消失。

    有没有办法检测用户何时打开iOS 7中文字母键盘并打开预测?通过这种方式,我们可以将元素上移一点以补偿它。

    2 回复  |  直到 9 年前
        1
  •  2
  •   n00bProgrammer    10 年前

    //查看DidLoad

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardFrameDidShow:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardFrameWillChange:)
                                                 name:UIKeyboardWillChangeFrameNotification
                                               object:nil];
    
    - (void)keyboardFrameDidShow:(NSNotification *)notification
    {
    
        CGRect keyboardFrame;
        [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
        CGRect inputPanelFrame = self.inputPanel.frame;
    
         UIView *superView = [ self.inputPanel superview];
    
        inputPanelFrame.origin.y = (superView.frame.size.height -(keyboardFrame.size.height+self.inputPanel.frame.size.height));
    
        self.inputPanel.frame = inputPanelFrame;
    }
    
    
    - (void)keyboardFrameWillChange:(NSNotification *)notification
    {
        CGRect keyboardFrame;
        [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
        CGRect inputPanelFrame = self.inputPanel.frame;
          UIView *superView = [ self.inputPanel superview];
        inputPanelFrame.origin.y = (superView.frame.size.height -(keyboardFrame.size.height+self.inputPanel.frame.size.height));
    
        self.inputPanel.frame = inputPanelFrame;
    
    }
    
        2
  •  1
  •   Jeremy H    10 年前

    想通了!首先收听UIKeyboardWillChangeFrameNotification。

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

    通知将发送一个包含多个键盘参数的字典。UIKeyboardFrameEndUserInfoKey的高度将为您提供键盘高度的实时更新,包括来自亚洲键盘的预测。

    - (void)changed:(NSNotification *)notification {
        NSDictionary *keyboardInfo = [notification userInfo];
        NSValue *keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
        float keyboardHeight = keyboardFrameBeginRect.size.height;
    }