iOS 8 获取键盘高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26110878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 8 get keyboard height
提问by James
I'm having trouble reliably getting the actual height of the keyboard. I'm adding a custom toolbar that is meant to be placed directly above the keyboard. To do so with layout constraints, I've added a static space constraint between the bottom of the screen and the bottom of the toolbar. When the keyboard is displayed, I modify this constraint to be the height of the keyboard, which varies quite a bit in iOS 8.
我无法可靠地获得键盘的实际高度。我正在添加一个自定义工具栏,旨在直接放置在键盘上方。为了实现布局约束,我在屏幕底部和工具栏底部之间添加了一个静态空间约束。当显示键盘时,我将此约束修改为键盘的高度,这在 iOS 8 中变化很大。
To resize the spacer, I currently use the following method that gets fired on UIKeyboardDidShowNotification
要调整垫片的大小,我目前使用以下方法触发 UIKeyboardDidShowNotification
-(void)keyboardDidShow:(NSNotification*)notification
{
CGFloat height = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].height;
self.shimConstraint.constant = height;
[self.view layoutIfNeeded];
}
This does resize the constraint correctly sometimes, but often the keyboard height returned is entirely incorrect. What is the best way to reliably get the height of the keyboard once it is displayed?
这有时会正确调整约束大小,但通常返回的键盘高度完全不正确。一旦显示,可靠地获得键盘高度的最佳方法是什么?
EDIT: The application allows the user to switch between a number of different input fields. Some of them have autocorrect disabled, so the autocorrect bar may or may not be hidden, and incorrect heights are returned when switching fields.
编辑:该应用程序允许用户在多个不同的输入字段之间切换。其中一些已禁用自动更正,因此自动更正栏可能会或可能不会隐藏,并且在切换字段时会返回不正确的高度。
回答by Kris Gellci
The way you are doing it is correct but you might want to use UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey. The end user key is the frame of the keyboard when it is done animating and on the screen, thus you know it will be that frame, while the begin key might not always match what the keyboard frame will be when it is shown.
您这样做的方式是正确的,但您可能希望使用 UIKeyboardFrameEndUserInfoKey 而不是 UIKeyboardFrameBeginUserInfoKey。最终用户键是完成动画并显示在屏幕上的键盘框架,因此您知道它将是该框架,而开始键可能并不总是与显示时键盘框架相匹配。
There might also need to be extra considerations on orientation of the device, I have not looked into that, but for portrait, it should work.
可能还需要额外考虑设备的方向,我没有研究过,但对于纵向,它应该可以工作。
回答by Jason Kirchner
The correct way to do this is to use the inputAccesorryView property of the UITextField.
正确的方法是使用 UITextField 的 inputAccesorryView 属性。
The inputAccessoryView is automatically added above the keyboard regardless of keyboard size.
无论键盘大小如何, inputAccessoryView 都会自动添加到键盘上方。
As the documentation states:
正如文档所述:
The default value of this property is nil. Assigning a view to this property causes that view to be displayed above the standard system keyboard (or above the custom input view if one is provided) when the text field becomes the first responder. For example, you could use this property to attach a custom toolbar to the keyboard
此属性的默认值为 nil。当文本字段成为第一响应者时,将视图分配给此属性会导致该视图显示在标准系统键盘上方(或自定义输入视图上方,如果提供)。例如,您可以使用此属性将自定义工具栏附加到键盘
See more information here https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html
回答by Dhaval
You might need to do [self.view setNeedsLayout]
instead of [self.view layoutIfNeeded]
.
您可能需要执行[self.view setNeedsLayout]
而不是[self.view layoutIfNeeded]
.