当UITableView嵌套在UIScrollView中时如何触发scrollsToTop
我想知道当 UITableView 嵌套在也观察此函数的 UIScrollView 中时,是否有人有幸从用户点击状态栏触发 UITableView 的 scrollsToTop (或通过任何其他方式)?我知道这可能不是一个好的做法,但在这种情况下,用户体验类型需要这种类型的层次结构。
不管怎样,我已经看到了一大堆提案,从私有方法(显然不会发生)到在状态栏上添加假窗口(也不会发生)。
I was wondering if anyone had had any luck triggering scrollsToTop (or by any other means) for a UITableView from the user tapping on the status bar when the UITableView is nested inside a UIScrollView that also observes this function? I know this probably isn't good practice, but in this instance the UX kind of calls for this type of hierarchy.
Either way, I've seen a whole bunch of proposals ranging from private methods (obviously not going to happen) to adding fake windows over the status bar (also not going to happen).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,所以这里的答案有两个方面:
在同一个 UIView 上,不能有多个 UIScrollView(或从 UIScrollView 派生或使用 UIScrollView 的类 - 即 UITableView),并且属性 scrollsToTop 设置为 YES。选择您想要拥有该功能的一项,并确保其他所有都没有
例如,执行以下操作:
实现 UIScrollView 委托方法
scrollViewShouldScrollToTop:
并返回 YES
如果调用 UIScrollView 是 UITableView。支持 此答案提及非多个
scrollsToTop
选项。Ok, so the answer here is two fold:
You cannot have more than one UIScrollView (or classes deriving from or using UIScrollView - i.e. UITableView) on the same UIView with the property scrollsToTop set to YES. Pick the one you want to have the feature and make sure all others are no
For example, do this:
Implement the UIScrollView delegate method
scrollViewShouldScrollToTop:
andreturn YES
if the calling UIScrollView is the UITableView.Props to this answer for mentioning the non-multiple
scrollsToTop
option.只是想分享我编写的一个有助于调试这些情况的小函数。正如其他人所提到的,您必须确保只有一个滚动视图打开了scrollsToTop。如果您嵌入复杂的视图层次结构,则可能很难找出哪个滚动视图是罪魁祸首。只需在创建视图层次结构后调用此方法,就像在 viewDidAppear 中一样。 level 参数只是为了帮助缩进,你应该用 0 作为种子
。在视图控制器的主视图上调用它。
在日志中,您应该看到>>scrollsToTop<<<在每个打开它的视图旁边,可以轻松找到错误。
Just wanted to share a little function I wrote that helps debug these situations. As others have mentioned, you have to make sure only ONE scroll view has scrollsToTop turned on. If you embed complex view hierarchies it may be difficult to figure out which scroll view is the culprit. Just call this method after your view hierarchy is created, like in viewDidAppear. The level parameter is just to help indentation and you should seed it off with 0.
Call it on your view controller's main view.
In the log, you should see >>>scrollsToTop<<< next to every view that has it turned on, making it easy to find the bug.
帮助我解决此问题的一件事是:
UIScollView
是否设置为scollsToTop = NO
添加子视图控制器作为子视图控制器addChildViewController:
仅将视图作为子视图添加到父视图中是不够的。One thing that helped me fix this problem is:
UIScollView
in the view hierarchy is set toscollsToTop = NO
addChildViewController:
It is not sufficient to only add the view as a subview to the parent's view.希望对你有帮助:)
Hoping to help you :)