键盘大小由NSNotificationCenter提供 [英] keyboard size given by NSNotificationCenter

查看:92
本文介绍了键盘大小由NSNotificationCenter提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在从UISearchBar调用的键盘上添加一个accessoryView。由于UISearchBar没有实现这个属性,我刚刚创建了一个toolBar。关于此事的 Apple的文档,我已经决定了使用通知中心不仅要知道键盘何时被调用,还要知道键盘的大小,这取决于方向。

I want to add a accessoryView on a keyboard called from a UISearchBar. Since UISearchBar does not implement this property, I've just created a toolBar. Following Apple's documentation on the matter, I've decided to use notification center not only to know when the keyboard is called but also to know the size of the keyboard, which changes depending on the orientation.

我已经关注了文档上的示例,在 keyboardWasShown 方法中,我调用一个动画,它将在键盘顶部显示工具栏。这样的事情:

I've followed the example on the documentation and, on the keyboardWasShown method, I call an animation which will show the toolBar on top of the keyboard. Something like this:

-(void)keyboardWasShown:(NSNotification*)aNotification {

    NSDictionary *info=[aNotification userInfo];
    CGSize keyboardSize=[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

     NSLog(@"width: %.1f; heigth: %.1f", keyboardSize.width, keyboardSize.height );

    [self showAccessoryView:keyboardSize.height];


}

并且在动画中我设置了工具栏的框架如下:

and, on the animation I set the frame of the toolbar like this:

 self.auxiliaryKeyboardBar.frame=CGRectMake(0, self.view.frame.size.height-(44+kbh), self.view.frame.size.width, 44);

其中44是工具栏的静态高度,kbh是从中传递的keyboard.size.heigth上面的方法。

where 44 is the static heigth of the toolbar and the kbh is the keyboard.size.heigth passed from the method above.

我观察到的问题是 userInfo 字典给出的键盘大小总是提到纵向方向。因此,纵向方向的NSLog是:

The problem I'm observing is that the keyboard Size given by the userInfo Dictionary is always refered to the portrait orientation. So, the NSLog on portrait orientation is:

宽度:320.0;高度:216.0 ,这是好的

但是当我将方向更改为横向并且我调用键盘时,NSLog如下:

but when I change the orientation to landscape and I call the keyboard, the NSLog is as follows:

宽度:162.0;高度:480.0 ,这会使工具栏超出范围。

width: 162.0; heigth: 480.0, which puts the toolbar out of scope.

所以,我最后在调用动画之前添加了一个条件,比如这个:

so, I ended up adding a conditional before calling the animation, such as this:

if ([self deviceIsPortrait]==YES) {
        [self showAccessoryView:keyboardSize.height];
    }else if ([self deviceIsPortrait]==NO) {
        [self showAccessoryView:keyboardSize.width];
    }

我现在想知道我是否做错了什么,因为我正好遵循Apple的例子,以避免依赖键盘高度(作为一个浮点数),最后我不得不添加一个方向条件。

I am now wondering whether or not I'm doing something wrong, because I'm following Apple's example precisely to avoid dependence on the keyboard heigth (as a float) and I ended up having to add a orientation conditional anyway.

关于什么会有什么想法在这里?

Any ideas on what's going on here?

推荐答案

我认为你所缺少的是:

CGRect keyboardFrameConverted = [mainSubviewOfWindow convertRect:keyboardFrame fromView:window];

这有点脱离背景,所以这是完整的实现:

That's a bit out of context, so here is the full implementation:

- (void) keyboardDidShow:(NSNotification*)notification {
    CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSLog(@"keyboard frame raw %@", NSStringFromCGRect(keyboardFrame));

    UIWindow *window = [[[UIApplication sharedApplication] windows]objectAtIndex:0];
    UIView *mainSubviewOfWindow = window.rootViewController.view;
    CGRect keyboardFrameConverted = [mainSubviewOfWindow convertRect:keyboardFrame fromView:window];
    NSLog(@"keyboard frame converted %@", NSStringFromCGRect(keyboardFrameConverted));
}

这篇关于键盘大小由NSNotificationCenter提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆