我正试图获得iOS键盘的高度.我已经完成并使用了涉及订阅通知的方法,如下所示:https: //gist.github.com/philipmcdermott/5183731
- (void)viewDidAppear:(BOOL) animated {
[super viewDidAppear:animated];
// Register notification when the keyboard will be show
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
// Register notification when the keyboard will be hide
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
CGRect keyboardBounds;
[[notification.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardBounds];
// Do something with keyboard height
}
- (void)keyboardWillHide:(NSNotification *)notification {
CGRect keyboardBounds;
[[notification.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardBounds];
// Do something with keyboard height
}
Run Code Online (Sandbox Code Playgroud)
这适用于用户实际显示键盘的情况.
我的问题:我有另一种观点,我们称它为micView,可以在键盘出现之前显示.用户可以在键入之前选择使用麦克风.我希望micView与键盘的高度相同,这就是我需要键盘高度的原因,但是在键盘被迫出现之前我需要它.因此,在我需要读取高度值之前,未达到UIKeyboardWillShowNotification.
我的问题是:如何在没有键盘出现的情况下通过Notifications或其他方法获得键盘的高度.
我考虑过明确强制键盘出现在viewDidLoad中,这样我就可以将一个实例变量设置为该值,然后将其隐藏并删除两者的动画.但这真的是唯一的方法吗?