iPhone SDK中防止光标移到键盘后面的最简单方法?
我有一个包含多个文本字段的表格。点击任何 UITextField
都会弹出键盘,如果需要,视图会自动滚动以将光标移到键盘上方。
我在那里有一个方法,每当用户在编辑文本字段时按下 Return 键时就会调用该方法,我只需将第一响应者状态移动到其中的下一个文本字段即可。但是,如果它恰好位于键盘后面,则它不会向上滚动并显示出来。
我知道这涉及使用 UIView
的 beginAnimations
方法并更改视图的位置或大小,但我不知道如何计算视图应移动多少等.?有没有简单的方法可以做到这一点?我认为苹果没有在文档中解释这一点,而且我已经尝试过谷歌搜索,但无法想出一个像样的解决方案。
任何帮助将不胜感激。
谢谢。
I have a table with several text fields in it. Tapping on any of the UITextField
s brings up the keyboard and the view scrolls automatically to bring the cursor above the keyboard if required.
I have a method in there that gets called whenever the user presses the Return key while editing a text field and I simply move the first responder status to the next text field in that. However, if it happens to be behind the keyboard, it does not scroll up and reveal itself.
I know this involves using the beginAnimations
method of UIView
and changing the position or size of the view but I don’t know how to calculate how much the view should move, etc.? Is there an easy way to do this? I don’t think Apple has explained this in the documentation and I’ve already tried Googling, but couldn’t come up with a decent solution.
Any help would be greatly appreciated.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到了解决方案,感谢此处的“mvds”以及对文档的一些挖掘。这就是我修改后的“scrollViewToCenter”方法的样子:
每当用户点击任何文本字段(或者以编程方式使其成为第一响应者)时,我都会通过将其添加为“UITextFieldTextDidBeginEditingNotification”通知的选择器来调用它:
调用该方法时,它首先确定七个文本字段中的哪一个是活动字段,然后使用“convertPoint:toView:”方法将其原点转换为“self.view”上的点。一旦获得文本字段的起始点,我就会计算它是否低于屏幕上的中点,如果是,则使用“setContentOffset”方法相应地偏移表格视图。
我发布了如此详细的信息,因为我必须竭尽全力去了解它是如何工作的,并且我认为这可能会节省其他人的时间。我希望它有用。
Found a solution to this, thanks to ‘mvds’ here and some digging around in the documentation. This is what my revised ‘scrollViewToCenter’ method looks like:
I call it whenever the user taps into any of the text fields (or it has been made the first responder programmatically) by adding it as a selector for the ‘UITextFieldTextDidBeginEditingNotification’ notification thusly:
When the method is called, it first determines which of the seven text fields is the active one and then, using the ‘convertPoint:toView:’ method, converts its origin point to a point on ‘self.view’. Once I have the point where the text field originates from, I calculate if it is below the halfway point on the screen and, if so, offset the table view accordingly using the ‘setContentOffset’ method.
I’ve posted this in so much detail because I had to go to great lengths to find out how this works and I figured it might save someone else that time. I hope it was useful.
AFAIK 确实没有开发人员友好的方法来做到这一点。从另一个项目复制粘贴,这就是我想到的:
windowleft是键盘出来时剩下的px数。这假设您有一个名为
scrollView
的UIScrollView
。然后只需调用
edit: 注意:要使其正常工作,传递的
UIView
最好应该是scrollView
的子视图。AFAIK there is indeed no developer friendly way to do this. Copy&pasting from another project, this is what I came up with:
windowleft is the number of px left when the keyboard comes out. This assumes you have a
UIScrollView
calledscrollView
.And then just call
edit: Note: for this to work, the
UIView
passed should preferably be a subview of thescrollView
.