UIKeyboardBoundsUserInfoKey 已弃用,该使用什么替代?
我正在使用 3.2 sdk 开发 iPad 应用程序。我正在处理获取键盘大小以防止我的文本字段隐藏在其后面。
我在 Xcode 中收到警告 -> UIKeyboardBoundsUserInfoKey 已弃用,我应该使用什么来避免收到此警告?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我使用了之前提供的解决方案,但仍然遇到问题。这是我想出的:
I played with the previously offered solution but still had issues. Here's what I came up with instead:
来自文档:
Apple 建议实现如下所示的便捷例程(可以作为
UIScreen
的类别添加来实现):请 窗口调整键盘框架大小属性。
我采取了不同的方法,其中涉及检查设备方向:
From the documentation for
UIKeyboardBoundsUserInfoKey
:Apple recommends implementing a convenience routine such as this (which could be implemented as a category addition to
UIScreen
):to recover window-adjusted keyboard frame size properties.
I took a different approach, which involves checking the device orientation:
您只需使用以下代码:
You simply use this code:
以下代码修复了 Jay 的回答 中的一个问题,该问题假设
UIKeyboardWillShowNotification
在以下情况下不会再次触发:键盘已经存在。使用日语/中文键盘输入时,即使键盘已经存在,iOS 也会使用新的键盘框架触发额外的
UIKeyboardWillShowNotification
,从而导致self.textView
的高度升高在原始代码中被第二次减少。这将 self.textView 减少到几乎为零。然后就不可能从这个问题中恢复了,因为下次键盘关闭时我们只会期望一个
UIKeyboardWillHideNotification
。下面的代码只是计算
self.textView
的最大可能高度,而不是像原始代码那样根据键盘是否显示/隐藏来减去/添加self.textView
的高度code> 减去屏幕上键盘的高度后。这假设 self.textView 假设填充视图控制器的整个视图,并且没有其他子视图需要可见。
另外,不要忘记在 viewDidLoad 中注册键盘通知:
关于将调整大小代码分为两部分
textView 调整大小代码分为两部分的原因(
resizeTextViewWithKeyboardNotification:
和resizeViewToAccommodateKeyboardFrame: withAnimationDuration:animationCurve:
) 是为了解决键盘通过从一个视图控制器推送到另一个视图控制器而持续存在时的另一个问题(请参阅 当 iOS 键盘在控制器之间保持打开状态时,如何检测它?)。由于在推送视图控制器之前键盘已经存在,因此 iOS 不会生成额外的键盘通知,因此无法根据这些键盘通知调整
textView
的大小。因此,调整
self.textView
大小的上述代码(以及原始代码)仅在加载视图之后显示键盘时才有效。我的解决方案是创建一个存储最后一个键盘坐标的单例,并在 viewController 的
- viewDidAppear:
上调用:UASKeyboard
是我的单例。理想情况下,我们应该在- viewWillAppear:
中调用它,但是根据我的经验(至少在 iOS 6 上),我们需要在中使用
在视图完全可见之前,无法正确地将键盘框架转换为视图坐标。convertRect:fromView:
方法>resizeViewToAccommodateKeyboardFrame:withAnimationDuration:animationCurve:The following code fixes an issue in Jay's answer, which assumes that
UIKeyboardWillShowNotification
will not fire again when the keyboard is already present.When typing with the Japanese/Chinese keyboard, iOS fires an extra
UIKeyboardWillShowNotification
with the new keyboard frame even though the keyboard is already present, leading to the height of theself.textView
being reduced a second time in the original code.This reduces
self.textView
to almost nothing. It then becomes impossible to recover from this problem since we will only expect a singleUIKeyboardWillHideNotification
the next time the keyboard is dismissed.Instead of subtracting/adding height to
self.textView
depending on whether the keyboard is shown/hidden as in the original code, the following code just calculates the maximum possible height forself.textView
after subtracting the height of the keyboard on screen.This assumes that
self.textView
is suppose to fill the entire view of the view controller, and there's no other subview that needs to be visible.Also, don't forget to register the keyboard notifications in viewDidLoad:
About splitting the resizing code into two parts
The reason why the textView resizing code is split into two parts (
resizeTextViewWithKeyboardNotification:
andresizeViewToAccommodateKeyboardFrame:withAnimationDuration:animationCurve:
) is to fix another issue when the keyboard persists through a push from one view controller to another (see How do I detect the iOS keyboard when it stays up between controllers?).Since the keyboard is already present before the view controller is pushed, there's no additional keyboard notifications being generated by iOS, and thus no way to resize the
textView
based on those keyboard notifications.The above code (as well as the original code) that resizes
self.textView
will thus only work when the keyboard is shown after the view has been loaded.My solution is to create a singleton that stores the last keyboard coordinates, and on
- viewDidAppear:
of the viewController, call:UASKeyboard
is my singleton here. Ideally we should call this in- viewWillAppear:
, however in my experience (at least on iOS 6), theconvertRect:fromView:
method that we need to use inresizeViewToAccommodateKeyboardFrame:withAnimationDuration:animationCurve:
does not properly convert the keyboard frame to the view coordinates before the view is fully visible.只需使用
UIKeyboardFrameBeginUserInfoKey
或UIKeyboardFrameEndUserInfoKey
键而不是UIKeyboardBoundsUserInfoKey
Just use the
UIKeyboardFrameBeginUserInfoKey
orUIKeyboardFrameEndUserInfoKey
key instead ofUIKeyboardBoundsUserInfoKey
@Jason,除了一点之外,你的代码如果很好的话。
目前,您实际上并未对任何内容进行动画处理,视图将简单地“弹出”到其新的 size.height。
您必须指定要进行动画处理的状态。动画是一种(从状态)->(到状态)的东西。
幸运的是,有一种非常方便的方法可以将视图的当前状态指定为(来自状态)。
如果您在 beginAnimations:context: 之后添加该行,您的代码将完美运行。
@Jason, you code if fine except for one point.
At the moment you are not actually animating anything and the view will simply `pop' to its new size.height.
You have to specify a state from which to animate. An animation is a sort of (from state)->(to state) thing.
Luckily there is a very convenient method to specify the current state of the view as the (from state).
If you add that line right after beginAnimations:context: your code works perfectly.
这里有一个很好的细节
http://i-phone -dev.blogspot.com/2012/01/ Different-way-to-show-keyboard-and.html
Here is a good details
http://i-phone-dev.blogspot.com/2012/01/different-way-to-show-keyboard-and.html
它的工作原理是这样的
这是保存按钮底部的约束
在viewDidLoad里面
这是我们需要的功能
Its worked like this
This is the constraint of the save button bottom
Inside viewDidLoad
This is the functions we need