通过点击状态栏滚动到 UITableView 顶部
我知道有大量代码可以将桌面视图滚动到顶部,但我想在点击顶部状态栏时执行此操作,就像在 Apple 的本机应用程序中一样。这可能吗?
I know there's tons of code out there to scroll a tableview to the top, but I want to do this when the top status bar is tapped, just like in Apple's native apps. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以免费获得它,但您应该检查
UITableView
的scrollsToTop
属性是否为 YES。当您将
UIScrollView
(或后代类,如 UITextView)对象嵌入到另一个UIScrollView
类(如UITableView
)中时,这不起作用。在这种情况下,请将嵌入的UIScrollView
类上的scrollsToTop
设置为 NO。然后点击状态栏行为就会起作用。You get this for free, but you should check that the
scrollsToTop
attribute of yourUITableView
is YES.When this does NOT work is when you have a
UIScrollView
(or descendant class like UITextView) object embedded inside anotherUIScrollView
class (likeUITableView
). In this case, setscrollsToTop
on the embeddedUIScrollView
class to NO. Then the tap-the-status-bar behavior will work.如果您来自 Google 并且需要完整的清单:
If you came from Google and need a complete checklist:
使用其他答案中给出的信息,我将以下代码添加到 UITableViewController 中,使其正常工作:
这将查看 UITableViewController 层次结构中的所有视图,并关闭所有拦截触摸事件的 UITextView 上的scrollsToTop。然后,确保 tableView 仍然会接收触摸。
您可以修改它以迭代也可能拦截的其他 UITableViews / UIScrollViews / UITextViews 。
希望这有帮助!
Using the information given in other answers, I added the following code to my UITableViewController get it to work:
This looks through all the views in the UITableViewController's hierarchy and turns off scrollsToTop on all the UITextViews that were intercepting the touch event. Then, ensured the tableView was still going to receive the touch.
You can mod this to iterate through other UITableViews / UIScrollViews / UITextViews that may be intercepting as well.
Hope this helps!
我遇到了同样的问题,但通过以下步骤解决了:
如果您的视图控制器/导航控制器作为子视图添加到另一个视图控制器上,请确保将其设置为子控制器。
I had the same problem but fixed by following steps:
If your view controller/ navigation controller is added as a subview on another view controller, Make sure you set it as a child Controller.
我知道这是一个相当老的问题,但希望这能有所帮助。按照@MarkGranoff所说,如果多个UIScrollView或其子类将其设置为YES(默认值),则scrollsToTop将不起作用,健全性检查可能值得检查谁实际上搞乱了这一行为。下面的简单方法循环遍历视图的子视图并记录视图中所有 UIScrollView 的scrollsToTop 值。最好在 viewDidAppear 方法中调用。
这确实只是一个调试代码。一旦找到每个 UIScrollView 子类的scrollsToTop 值,只需确保只有一个设置为 YES。
I know this is quite an old one but hope this can help. Following what @MarkGranoff said, the scrollsToTop doesn't work if more than one UIScrollView, or its subclasses, has got it set to YES (default value), a sanity check is probably worth to check who's actually messing up with this behaviour. The simple method below loop over the subviews of your view and logs the scrollsToTop value of all the UIScrollView in your view. Preferably to be called in your viewDidAppear method.
This is just a debug code indeed. Once you find the scrollsToTop value for each one of the UIScrollView subclasses just make sure only one is set to YES.
正如 Mark 所说,你只能将 UIScrollView 的一个子类(通常是表视图)的 scrollsToTop 属性设置为 TRUE。可能您还有其他的,通常是您视图中的 UITextView。只需将它们的scrollsToTop 属性设置为FALSE 即可。
Like Mark said, you can only have one subclass of UIScrollView (usually the table view) that has the scrollsToTop property set to TRUE. Likely you have others, typically UITextView in your view. Just set their scrollsToTop property to FALSE and you're good to go.
在 UIScrollView 头文件上:
On UIScrollView header file:
例如,如果您有像这样的标签的表格视图和滚动视图
你应该在
viewDidLoad
中做类似的事情For example if you have a table view and scroll view like tags like this
you should make something like this in
viewDidLoad
可以加载多个 UIScrollView 后代,例如:在包含 UITableViews 的每个页面上都有一个 UIPageViewcontroller。
默认情况下,scrollsToTop 属性为true。
因此,除了处理嵌套的UIScrollViews的scrollsToTop属性之外,您还应该执行以下操作:
这样只有一个顶级UITableView的scrollsToTop将被设置为true。
There can be multiple UIScrollView descendants loaded eg.: having a UIPageViewcontroller on each page containing UITableViews.
The scrollsToTop property is true by default.
So in addition to handling nested UIScrollViews' scrollsToTop property, you should do the following:
This way only one top level UITableView's scrollsToTop will be set to true.