使视图向上滑动以便为键盘腾出空间?
我刚刚开始学习 iPhone 应用程序编程,我似乎不知道如何在键盘出现时让视图滑开(这样你仍然可以看到正在输入的文本字段)。它是如何完成的?
I just started learning to program iPhone apps and I can't seem to figure out how to make the view slide out of the way when the keyboard appears (so you can still see the text field you're typing in). How is it done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果视觉上没问题,最简单的方法是移动整个 self.view.frame,然后在完成后将其向下移动。
If it is OK visually, the easiest is to move the entire self.view.frame and then move it back down when finished.
一种方法是将所有内容包含在 UIScrollView 中,然后向上滚动内容。另一种方法是自己移动视图,通常在核心动画的帮助下使其看起来更漂亮。
一个好的起点是文档。甚至还有一个部分标记为移动位于键盘下方的内容,这将指向你的方向是正确的。
One way to do it is to contain everything in a UIScrollView and then scroll the contents upward. Another way is to move the view yourself, usually with the help of Core Animation to make it look nice.
A good place to start is with the documenation. There's even a section helpfully labelled Moving Content That Is Located Under the Keyboard which will point you in the right direction.
假设您需要在标签为 4 的文本字段上向上移动视图(当您有超过 1 个 txt 字段并且其中之一被键盘覆盖时),然后使用 textField 委托方法
来移动您的视图。现在,要向下移动,您需要在 textField 另一个委托方法中编写代码。
如果是 textview,您需要一个按钮,而要向上移动视图,您需要此委托
并使用与 textField 相同的代码
。向下移动,您需要在导航栏中添加一个按钮,或者添加一个按钮。在工具栏中,并通过相同的动画将该工具栏设置在键盘上。对于按钮,您需要相同的代码来向下移动,这适用于文本字段。
希望这对您有帮助。
suppose you need to move up view on a text field which tag is 4(when you have more than 1 txt fields and one of them covered by the keyboard) then use textField delegate method
this moves your view. now for moving down you need code in textField anothe delegate method
In case of textview you need to a button and for moving up your view you need this delegate
and use same code as textField
And moving down you need a button either in navigation bar or add in toolbar and set that tool bar over the keyboard by same animation.For button you need same code for moving down which is applicable for textField.
Hope this helps you.
我发现使用键盘通知比使用 textFieldDidBeginEditing 和 textFieldDidEndEditing 的 UITextField 委托协议更适合我的应用程序。通知是keyboardWillShow 和keyboardWillHide。可以测试 UITextField 或 UITextView 是否需要视图随这些通知移动,然后有条件地移动视图。我的应用程序的优点是我有许多 UITextTield,并且当编辑从一个字段移动到另一个字段时,通知可以更轻松地将视图保持在键盘上方。
I found that using the keyboard notifications worked better for my application than using the UITextField delegate protocol of textFieldDidBeginEditing and textFieldDidEndEditing. The notifications are keyboardWillShow and keyboardWillHide. One can test for a UITextField or UITextView that would require the view to move withing these notifications and then conditionally move the view. The advantage in my application is that I have many UITextTields and the notifications make it easier to keep the view in place above the keyboard when editing moves from one field to the other.
}
}
使用 iOS 15 或更高版本时,您可以使用
UIView
的keyboardLayoutGuide
属性在键盘出现时自动调整视图底部的大小。自动布局约束示例,将 myView 置于屏幕键盘顶部:
更多信息和示例代码可在此处找到:https://developer.apple.com/documentation/uikit/keyboards_and_input/ adjustment_your_layout_with_keyboard_layout_guide
When using iOS 15 or newer, you can make use of the
keyboardLayoutGuide
property of theUIView
to automatically resize the bottom of a view, when the keyboard appears.Example auto layout constraint, to have myView on top of the onscreen keyboard:
More info and example code can be found here: https://developer.apple.com/documentation/uikit/keyboards_and_input/adjusting_your_layout_with_keyboard_layout_guide