简体   繁体   English

iOS 8获得键盘高度

[英]iOS 8 get keyboard height

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 为了调整spacer的大小,我目前使用以下方法在UIKeyboardDidShowNotificationUIKeyboardDidShowNotification

-(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. 其中一些禁用了自动更正,因此自动更正栏可能会隐藏,也可能不会隐藏,切换字段时会返回不正确的高度。

The way you are doing it is correct but you might want to use UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey. 你这样做的方式是正确的,但你可能想要使用UIKeyboardFrameEndUserInfoKey而不是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. 最终用户键是在完成动画和屏幕上时键盘的框架,因此您知道它将是该帧,而开始键可能并不总是与键盘帧显示时相匹配。

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. 可能还需要额外考虑设备的方向,我没有考虑过,但对于肖像,它应该工作。

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. 此属性的默认值为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 例如,您可以使用此属性将自定义工具栏附加到键盘

See more information here https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html 点击此处查看更多信息https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html

您可能需要[self.view setNeedsLayout]而不是[self.view layoutIfNeeded]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM