如何检测键盘类型的变化与新键盘类型的大小,类型,建议栏高度?

时间:2016-02-16 06:56:53

标签: ios objective-c uikeyboard custom-keyboard

有没有办法检测键盘类型的变化与大小,类型和建议栏高度,从英语键盘改为印地语,其中包含某种建议栏(见截图)。

普通英语键盘

enter image description here enter image description here

  

第一个问题

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

enter image description here enter image description here

  

第二个问题

更改为表情符号后 - 与英语相比,表情符号键盘的亮度稍高,因此键盘再次覆盖TextField

enter image description here

4 个答案:

答案 0 :(得分:5)

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

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

添加观察者

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

    NSLog(@"%@", notification);
}
带记录器的

选择器

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;
}}

userInfo字典内容

 _onNewFlowCreated(newFlow) {
        let url= '/#/flows/'+ newFlow.uid +'/edit';
        //window.history.pushState({}, '', url);
         window.location.href= url;
    }

答案 1 :(得分:3)

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

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

使用UIKeyboardFrameEndUserInfoKey而不是使用UIKeyboardFrameBeginUserInfoKey。

答案 2 :(得分:2)

Swift 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)
     }
}

答案 3 :(得分:0)

你需要按照屏幕上的显示获得键盘的高度

  • 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;

实施 keyboardWillShow keyboardWillHide 以对键盘高度的当前变化作出反应:

- (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;
}