Xcode/iOS5:当键盘出现时向上移动 UIView
当显示键盘时,我想向上移动我的视图。键盘(高度:216)应该用它的高度来推高我的视野。这可以通过简单的代码实现吗?
I'd like to move up my view, when the keyboard is shown. The keyboard (height: 216) should push up my view with it's height. Is this possible with a simple code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
要
向上
移动视图,只需更改其中心
即可。首先,将原始点保留在CGPoint
属性中。然后,当键盘出现时根据需要进行更改:
最后,当键盘隐藏时恢复它:
根据需要添加动画糖
您有不止一种方法可以知道键盘何时出现。
观察 UIKeyboardDidShowNotification 通知。
使用
UIKeyboardDidHideNotification
执行相反的操作。-或-
实现 UITextFieldDelegate
检测何时编辑开始/结束以移动视图。
根据实际的文本字段,您可能需要跟踪用户在哪个字段中编辑,添加一个计时器以避免过多移动视图。
To move the view
up
, just change itscenter
. First, keep the original one in aCGPoint
property.Then, change as needed when keyboard shows up:
Finally, restore it when keyboard is hidden:
Add animation sugar as you wish
You have more than one way to know when the keyboard appears.
Observing UIKeyboardDidShowNotification notification.
Do the opposite with
UIKeyboardDidHideNotification
.-OR-
Implement UITextFieldDelegate
Detect when editing begin/end to move views around.
Depending on the actual text fields you may need to track in which field is the user editing, add a timer to avoid moving views too much.
这样做。
键盘可见后使用此代码
do like this.
after keyboard visible use this code
我以与 djromero 类似的方式执行此操作,只不过我调整了视图的框架原点而不是其中心。
我正在移动的视图是 UIScrollView,我希望它相对于 UITextField 元素移动,以便文本字段始终显示。该文本字段的位置可能会根据滚动视图的偏移量而变化。
所以我的代码如下所示:
视图控制器是一个 UITextFieldDelegate,并且还订阅 UIKeyboardDidShowNotification,以便我们能够访问键盘的大小。
当键盘显示时,我们计算 UITextField 的相对偏移量(根据滚动偏移进行调整)和键盘,然后它们更改 UIScrollView 的原点,以便它移动得足以让 UITextField 仍然显示。
如果即使出现键盘,UITextField 仍会显示,则原点不会改变。
I did this in a similar fashion to djromero except that I adjusted the frame origin of the view rather than its centre.
The view that i'm moving is a UIScrollView, and I want it to move relative to a UITextField element, so that the text field always shows. The position of this text field can vary depending on the offset of the scroll view.
So my code looks like this:
The view controller is a UITextFieldDelegate and also subscribes to UIKeyboardDidShowNotification so that we are able to access the size of the keyboard.
When the keyboard shows, we calculate the relative offset of the UITextField (adjusted for scroll offset) and the keyboard and them change the origin of the UIScrollView so that it moves just enough for the UITextField still to be showing.
If the UITextField will still show even if the keyboard appears, then the origin does not change.
这是实现此目的最简单、最有效的方法:
添加以下常量:
将其添加到您的视图控制器:
并将这些方法添加到您的代码中:
This is the most easiest and efficient way to achieve this:
Add the following constants:
Add this to your view controller:
And add these methods to your code:
标记,苹果文档:
管理键盘 - 移动位于键盘下的内容
mark, apple document:
Managing the keyboard - Moving Content That Is Located Under the Keyboard
假设您有一些代码调用
[myTextFieldBecomeFirstResponder];
。您应该在这次通话之后立即移动您的视图。Presumably you have some code calling
[myTextField becomeFirstResponder];
. You should move your view just after this call.只需编辑这两个方法即可。
所有 d 代码的简单答案。在if语句中根据iphone更改值,如果iphone 4S将其更改为265,在didbeginediting中将320更改为240,如果iphone 5将其更改为350,在didbeginediting方法中将其保留为320,因为这是逻辑如果你明白的话
just edit these two methods.
simple answer to all d codes. In the if statement change the value according to the iphone i.e., if iphone 4S change it to 265 and in didbeginediting change the 320 to 240 and if iphone 5 change it to 350 and in didbeginediting method keep it as 320, because it is the logic if u understand
这是 Tendulkar 解决方案,但请记住原始框架尺寸并移除键盘。该解决方案适用于所有设备。
不要忘记设置 UITextField 委托!
This is Tendulkar solution, but keeping in mind original frame size and removing keyboard. This solution works for all devices.
Do not forget set UITextField delegate!
示例项目基于 苹果参考 键盘文档
H 文件:(不要忘记UITextFieldDelegate)
M 文件:
Sample project Based on Apple Reference Keyboard Documentation
H File: (Don't Forget the UITextFieldDelegate)
M file: