我正在尝试获取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
}
当用户实际显示键盘时,这个方法可以正常工作。
我的问题是:我有另一个视图,我们称之为micView,在键盘出现之前可能会出现。用户可能选择在打字之前使用麦克风。我希望micView与键盘具有相同的高度,这就是为什么我需要键盘的高度,但我需要在强制出现键盘之前获取它。因此,在我需要读取高度值之前,无法使用UIKeyboardWillShowNotification通知。
我的问题是:如何通过通知或其他方法获取键盘的高度,而不必让键盘出现。
我考虑过在viewDidLoad中显式强制出现键盘,以便我可以将一个实例变量设置为该值,然后隐藏它并摆脱两者的动画。但是,那真的是唯一的方法吗?