使 tableviewcell 在显示键盘时向上滚动
我有一个带有 UITextField 的 UITableViewCell 。在编辑模式下,当键盘出现时,我尝试编辑的行会被键盘重叠。理想情况下,该行应该向上滚动,以便我可以编辑单元格。
我彻底搜索了 stackoverflow,找到了几种不同的解决方案。其中大多数与向上移动或滚动视图的计算有关。现在有一个来自苹果的示例代码,名为 TaggedLocations。他们有完全相同的行为。并且没有代码执行任何复杂的计算来向上移动视图。
我还彻底检查了 IB 界面,也没有发现任何奇怪的事情。如果您下载并运行代码,它会完美地将行推到适合编辑的完美位置。
有谁知道 TaggedLocations 项目中有什么技巧可以如此优雅地做到这一点?项目地点: http://developer.apple.com/library/ios/#samplecode/TaggedLocations/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40008914
对于我所指的复杂计算,例如,看看以下线程:
I have a UITableViewCell with UITextField. In the edit mode, when the keyboard comes up, the row I am trying to edit gets overlapped by the keyboard. Ideally the row should scroll up so that I can edit the cell.
I have searched stackoverflow throughly and found several different solutions. Most of them has to do with calculations to move or scroll the view up. Now there is a sample code from apple called TaggedLocations. They have the exact same behavior. And there is no code doing any complex calculations to move the view up.
I also thoroughly checked the IB interface and could not find any fancy thing going on either. If you download and run the code, it beautifully pushes up the row to a perfect position for the edit.
Does anybody know what trick is there in the TaggedLocations project which does this so elegantly? Location of the project:
http://developer.apple.com/library/ios/#samplecode/TaggedLocations/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40008914
For the complex calculations I was referring, look at the following thread for example:
How to make a UITextField move up when keyboard is present?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
RootViewController.m 中的这一行是导致滚动的原因:
将其放入您的
cellForRowAtIndexPath:
方法中,它应该可以工作。This line in RootViewController.m is what causes the scroll:
Put that in your
cellForRowAtIndexPath:
method and it should work.我发现了问题所在。
最初我有
TabBarController
- UIViewController
-- UINavigationController
--- UITableViewController
- UIViewController
- -UINavigationController
--- UITableViewController
对于这种层次结构,当显示键盘时,表格单元格不会滚动。但是,如果我将层次结构更改为以下内容,则无需任何其他代码即可很好地进行汇总!
TabBar控制器
- UINavigationController
-- UITableViewController
- UINavigationController
-- UITableViewController
当我复制 Apple 代码时,我以某种方式搞乱了层次结构。现在切换到新的层次结构时,它工作得很好。
无需额外的代码或计算。
I figured out the problem.
Initially I had
TabBarController
- UIViewController
-- UINavigationController
--- UITableViewController
- UIViewController
- -UINavigationController
--- UITableViewController
With this kind of a hierarchy the table cells do not roll up when keyboard is displayed. But if I change the hierarchy to the following the rolling up happens just fine without any additional code!
TabBarController
- UINavigationController
-- UITableViewController
- UINavigationController
-- UITableViewController
When I duplicated the Apple code, I somehow messed up the hierarchy. Now on switching to the new hierarchy, it works fine.
No additional code or calculation necessary.
或者如果您有自己的控制器层次结构,则调用 viewWillAppear...那么表格视图将在键盘出现时自动滚动单元格以使其可见。
or call viewWillAppear if you have your own hierarchy of controllers... then table views will automatically scroll cells to be visible when keyboard appears.
我对 Apple 的示例代码 TaggedLocations 也有类似的困惑,尽管在我的例子中,我意识到我的表视图是由 UIViewController 子类而不是 UITableViewController 子类管理的,并且 UITableViewController 显然为您管理滚动。它运行得很好,所以很多复杂的计算都是不必要的。我希望我没有花那么多时间试图弄清楚它们。
我不明白为什么你描述的层次结构很重要。只要 UITableViewController 正在管理文本字段,它就应该自动处理滚动以避免键盘。
I had a similar confusion with Apple's sample code TaggedLocations, although in my case I realized that my table view was being managed by a UIViewController subclass as opposed to a UITableViewController subclass, and UITableViewController apparently manages the scrolling for you. It works quite well, so a lot of these complex calculations are unnecessary. I wish I didn't spend so much time trying to figure them out.
I don't see why the hierarchy you describe would matter. As long as the UITableViewController is managing the text fields, it should automatically take care of the scrolling to avoid the keyboard.