UIKeyboard回避和自动布局 [英] UIKeyboard avoidance and Auto Layout

查看:68
本文介绍了UIKeyboard回避和自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到iOS 6中的自动布局的重点,以及苹果工程师(查看WWDC 2012视频)的建议,我们不再直接操作视图的框架,如何避免


$ b


$ b

这看起来像是一个只有自动布局和NSLayoutConstraint的键盘?合理解决方案:一个键盘敏感布局示例 GitHub源代码),但是我看到的一个潜在问题是,

解决方案

我的想法是创建一个 UIView ,让我们称为键盘视图,并将其放在视图控制器的视图中。然后观察键盘框架更改通知 UIKeyboardDidChangeFrameNotification ,并将键盘框架与键盘视图匹配(我建议为动画更改)。观察此通知会处理您提到的旋转,并在iPad上移动键盘。



然后只需创建与此键盘视图相关的约束。



要让键盘框架正确翻译并旋转到视图坐标,请查看<$ c $的文档c> UIKeyboardFrameEndUserInfoKey 。


Given the focus on Auto Layout in iOS 6, and the recommendation by Apple engineers (see WWDC 2012 videos) that we no longer manipulate a views' frame directly, how would one go about avoiding the keyboard using only Auto Layout and NSLayoutConstraint?

Update

This looks like a reasonable solution: An example of keyboard sensitive layout (GitHub source) but one potential issue I see is what happens when a user rotates the device and the keyboard is already on screen?

解决方案

My idea is to create a UIView, let's call it keyboard view, and place it to your view controller's view. Then observe keyboard frame change notifications UIKeyboardDidChangeFrameNotification and match the frame of the keyboard to the keyboard view (I recommend to animate the change). Observing this notification handles the rotation you mentioned and also moving keyboard on iPad.

Then simply create your constraints relative to this keyboard view. Don't forget to add the constraint to their common superview.

To get the keyboard frame correctly translated and rotated to your view coordinates check out the docs for UIKeyboardFrameEndUserInfoKey.

这篇关于UIKeyboard回避和自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆