如何使用Apple iPhone SDK获得UIKeyboard尺寸 [英] How to get UIKeyboard size with Apple iPhone SDK
问题描述
有没有办法以编程方式获得UIKeyboard的大小。高度216.0f,景观高度162.0f。
Is there some way to get UIKeyboard size programatically. 216.0f height and 162.0f height in landscape.
以下似乎被贬低。有没有什么方法可以在3.0 iPhone OS SDK和4.0 iPhone OS SDK中都没有任何警告来执行此操作..
Following seem to be depricated. 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;
谢谢,
Tharindu
Thanks, Tharindu
推荐答案
您可以使用 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];
}
编辑2:
另外,请不要忘记在 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.
这篇关于如何使用Apple iPhone SDK获得UIKeyboard尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!