简体   繁体   English

在iPhone的键盘顶部添加文本框和按钮

[英]adding a textbox and a button at the top of the keyboard on iphone

I want to add a text box and a button beside it. 我想在其旁边添加一个文本框和一个按钮。 They will be at the bottom of the window. 它们将在窗口的底部。 Then, when I touch the textbox (to type something), keyboard will appear and the whole row (with textbox and button) scrolls up and the keyboard will be right below them. 然后,当我触摸文本框(键入内容)时,键盘将出现,整行(带有文本框和按钮)将向上滚动,并且键盘将位于它们的正下方。 Could you please let me know how can I do that? 您能告诉我该怎么做吗?

Is there any sample program? 有示例程序吗?

Thanks. 谢谢。

Matt Gallagher posted this on his blog: 马特·加拉格尔(Matt Gallagher)在他的博客上发布了此信息:

Sliding UITextFields around to avoid the keyboard 滑动UITextFields以避免键盘

It is a step by step example of exactly what you want. 这是您想要的逐步示例。

In the XCode documentation iPhone Application Programming Guide there is a section on "Moving Content That Is Located Under the Keyboard" that talks about receiving keyboard notifications when a keyboard is about to show. 在XCode文档《 iPhone应用程序编程指南》中,有一个关于“移动位于键盘下方的内容”的部分,该部分讨论在键盘将要显示时接收键盘通知。 There's code there to show you how to get the keyboard size (which varies depending on the orientation). 那里有代码向您展示如何获取键盘大小(取决于方向)。 I won't repeat it here. 我在这里不再重复。

You can use the same technique to get the UIKeyboardWillShowNotification notification and get the height of where the keyboard will end up. 您可以使用相同的技术来获取UIKeyboardWillShowNotification通知,并获取键盘结束处的高度。 That gives you the bottom edge of where your view needs to go, effectively putting it above the keyboard. 这样,您便可以将视图的底部边缘移到键盘上方,从而有效地将视图置于键盘上方。 So just put your textbox and button inside a view. 因此,只需将您的文本框和按钮放在视图中即可。 When you get the notification tell your view where it needs to go (keyboard height + height of the container view) and you're done. 收到通知后,告诉您的视图需要去哪里(键盘高度+容器视图的高度),您就完成了。 You'll also want to catch UIKeyboardWillHideNotification to move the view back to where it was, so keep track of the original container view position. 您还需要捕获UIKeyboardWillHideNotification以将视图移回原来的位置,因此请跟踪原始容器视图的位置。

It's pretty straightforward and it'll look nice, especially if you use a nice UIView animation effect and set the timing just right. 它非常简单明了,而且看起来不错,尤其是当您使用不错的UIView动画效果并正确设置时间时。

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

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