通过点击状态栏滚动到 UITableView 顶部

发布于 2024-12-01 07:30:39 字数 72 浏览 0 评论 0原文

我知道有大量代码可以将桌面视图滚动到顶部,但我想在点击顶部状态栏时执行此操作,就像在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

待"谢繁草 2024-12-08 07:30:39

您可以免费获得它,但您应该检查 UITableViewscrollsToTop 属性是否为 YES。

当您将 UIScrollView (或后代类,如 UITextView)对象嵌入到另一个 UIScrollView 类(如 UITableView)中时,这不起作用。在这种情况下,请将嵌入的 UIScrollView 类上的 scrollsToTop 设置为 NO。然后点击状态栏行为就会起作用。

You get this for free, but you should check that the scrollsToTop attribute of your UITableView is YES.

When this does NOT work is when you have a UIScrollView (or descendant class like UITextView) object embedded inside another UIScrollView class (like UITableView). In this case, set scrollsToTop on the embedded UIScrollView class to NO. Then the tap-the-status-bar behavior will work.

从来不烧饼 2024-12-08 07:30:39

如果您来自 Google 并且需要完整的清单:

  1. 检查您是否已在 UITableView 上设置了scrollsToTop=YES(根据 Mark 的建议)
  2. 确保您已在窗口中的所有其他 UITableViews / UIScrollViews / UITextViews 上设置了scrollsToTop=NO,这样他们就不会拦截点击。我发现自己多次打印出窗口中的所有视图来调试此...
  3. 确保您的表格视图位于窗口内的 0/0(x/y 坐标) - 这就是系统知道它的方式应该传递消息

If you came from Google and need a complete checklist:

  1. Check that you've set scrollsToTop=YES (per Mark's suggestion) on your UITableView
  2. Make sure that you've set scrollsToTop=NO on all OTHER UITableViews / UIScrollViews / UITextViews in your window, so that they're not intercepting the click. I've found myself printing out all the views in my window many times to debug this...
  3. Make sure that your table view is at 0/0 (x/y coordinates) within the window - this is how the system knows that it should pass the message
独夜无伴 2024-12-08 07:30:39

使用其他答案中给出的信息,我将以下代码添加到 UITableViewController 中,使其正常工作:

- (void)viewDidLoad
{
    [super viewDidLoad];

    for (UITextView *view in self.view.subviews) {
        if ([view isKindOfClass:[UITextView class]]) {
            view.scrollsToTop = NO;
        }
    }

    self.tableView.scrollsToTop = YES;
}

这将查看 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:

- (void)viewDidLoad
{
    [super viewDidLoad];

    for (UITextView *view in self.view.subviews) {
        if ([view isKindOfClass:[UITextView class]]) {
            view.scrollsToTop = NO;
        }
    }

    self.tableView.scrollsToTop = YES;
}

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!

怂人 2024-12-08 07:30:39

我遇到了同样的问题,但通过以下步骤解决了:

  1. 为要滚动到顶部的表视图设置scrollsToTop = YES。
  2. 对于所有其他表视图或集合视图或滚动视图设置scrollsToTop = NO。
  3. 如果您的任何 tableview 单元格具有 collection view 。确保将集合视图的scrollsToTop 设置为NO。

如果您的视图控制器/导航控制器作为子视图添加到另一个视图控制器上,请确保将其设置为子控制器。

I had the same problem but fixed by following steps:

  1. Set scrollsToTop = YES for tableview you wanted to scroll to top.
  2. set scrollsToTop = NO for all other tableview or collection view or scrollview.
  3. If any of your tableview cell has collection view . Make sure you set scrollsToTop to NO for the collection view as well.

If your view controller/ navigation controller is added as a subview on another view controller, Make sure you set it as a child Controller.

花伊自在美 2024-12-08 07:30:39

我知道这是一个相当老的问题,但希望这能有所帮助。按照@MarkGranoff所说,如果多个UIScrollView或其子类将其设置为YES(默认值),则scrollsToTop将不起作用,健全性检查可能值得检查谁实际上搞乱了这一行为。下面的简单方法循环遍历视图的子视图并记录视图中所有 UIScrollView 的scrollsToTop 值。最好在 viewDidAppear 方法中调用。

- (void)checkForScrollViewInView:(UIView *)view {
    for (UIView *subview in [view subviews]) {
        if ([subview isKindOfClass:[UIScrollView class]]) {
            NSLog(@"scrollsToTop enabled: %i in scroll view %@", ((UIScrollView *)subview).scrollsToTop, subview);
        }
        if (subview.subviews.count > 0) {
            [self checkForScrollViewInView:subview];
        }
    }
}

这确实只是一个调试代码。一旦找到每个 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.

- (void)checkForScrollViewInView:(UIView *)view {
    for (UIView *subview in [view subviews]) {
        if ([subview isKindOfClass:[UIScrollView class]]) {
            NSLog(@"scrollsToTop enabled: %i in scroll view %@", ((UIScrollView *)subview).scrollsToTop, subview);
        }
        if (subview.subviews.count > 0) {
            [self checkForScrollViewInView:subview];
        }
    }
}

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.

甜`诱少女 2024-12-08 07:30:39

正如 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.

水晶透心 2024-12-08 07:30:39

在 UIScrollView 头文件上:

// 当用户点击状态栏时,触摸下方距离状态栏最近的滚动视图将滚动到顶部,但前提是其 scrollsToTop 属性为 YES,其委托不会从 shouldScrollViewScrollToTop 返回 NO,并且它尚未位于顶部。
// 在 iPhone 上,仅当屏幕上有一个滚动视图且 scrollsToTop == YES 时,我们才会执行此手势。如果找到多个,则不会滚动任何一个。

On UIScrollView header file:

// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its scrollsToTop property is YES, its delegate does not return NO from shouldScrollViewScrollToTop, and it is not already at the top.
// On iPhone, we execute this gesture only if there's one on-screen scroll view with scrollsToTop == YES. If more than one is found, none will be scrolled.

乖乖 2024-12-08 07:30:39

例如,如果您有像这样的标签的表格视图和滚动视图

在此处输入图像描述

你应该在 viewDidLoad 中做类似的事情

self.tableView.scrollsToTop = true
self.tagsView.scrollsToTop = false

For example if you have a table view and scroll view like tags like this

enter image description here

you should make something like this in viewDidLoad

self.tableView.scrollsToTop = true
self.tagsView.scrollsToTop = false
垂暮老矣 2024-12-08 07:30:39

可以加载多个 UIScrollView 后代,例如:在包含 UITableViews 的每个页面上都有一个 UIPageViewcontroller。

默认情况下,scrollsToTop 属性为true

因此,除了处理嵌套的UIScrollViews的scrollsToTop属性之外,您还应该执行以下操作:

//When the view is loaded disable scrollsToTop, this view may not be the visible one
override func viewDidLoad() {
    super.viewDidLoad()
    ...
    tableView.scrollsToTop = false
}

//Now it's time to enable scrolling, the view is guaranteed to be visible
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    tableView.scrollsToTop = true
}

//Do not forget to disable scrollsToTop, making other visible UIScrollView descendant be able to be scrolled to top
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.tableView.scrollsToTop = false
}

这样只有一个顶级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:

//When the view is loaded disable scrollsToTop, this view may not be the visible one
override func viewDidLoad() {
    super.viewDidLoad()
    ...
    tableView.scrollsToTop = false
}

//Now it's time to enable scrolling, the view is guaranteed to be visible
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    tableView.scrollsToTop = true
}

//Do not forget to disable scrollsToTop, making other visible UIScrollView descendant be able to be scrolled to top
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.tableView.scrollsToTop = false
}

This way only one top level UITableView's scrollsToTop will be set to true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文