如何使用iOS获取UIKeyboard大小 [英] How to get UIKeyboard size with iOS

查看:74
本文介绍了如何使用iOS获取UIKeyboard大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过编程方式获取UIKeyboard大小.风景中的高度为216.0f,而高度为162.0f.

Is there some way to get UIKeyboard size programmatically. 216.0f height and 162.0f height in landscape.

以下似乎已被弃用.在3.0 iPhone OS SDK和4.0 iPhone OS SDK中都可以在没有任何警告的情况下执行某些操作.

Following seem to be deprecated. Is there some way that works without any warning in both 3.0 iPhone OS SDK and 4.0 iPhone OS SDK to do this..

CGSize keyBoardSize = [[[note userInfo]
                        objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size;

推荐答案

您可以改为使用 UIKeyboardFrameBeginUserInfoKey UIKeyboardFrameEndUserInfoKey userInfo词典中获取键盘大小.

You can get the keyboard size from the userInfo dictionary using the UIKeyboardFrameBeginUserInfoKey and the UIKeyboardFrameEndUserInfoKey instead.

这两个键返回一个NSValue实例,该实例包含一个CGRect,该实例在键盘的显示/隐藏动画的起点和终点均保持键盘的位置和大小.

These two keys return a NSValue instance containing a CGRect that holds the position and size of the keyboard at both the start and end points of the keyboard's show/hide animation.

为澄清起见,userInfo词典来自 NSNotification 实例.它已传递给您向观察员注册的方法.例如,

To clarify, the userInfo dictionary comes from an NSNotification instance. It's passed to your method that you register with an observer. For example,

- (void)someMethodWhereYouSetUpYourObserver
{
    // This could be in an init method.
    [[NSNotificationCenter defaultCenter] addObserver:self 
                    selector:@selector(myNotificationMethod:) 
                    name:UIKeyboardDidShowNotification 
                    object:nil];
}

- (void)myNotificationMethod:(NSNotification*)notification
{
    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
}

此外,请不要忘记在dealloc方法中以观察者身份离开自己!这是为了避免在通知中心释放对象后通知中心尝试通知您时发生崩溃.

Also, please don't forget to remove yourself as an observer in your dealloc method! This is to avoid a crash that would occur when the notification center tries to notify your object after its been freed.

这篇关于如何使用iOS获取UIKeyboard大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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