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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7165913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:13:52  来源:igfitidea点击:

Scroll to top of UITableView by tapping status bar

iosobjective-cscrolltableviewstatusbar

提问by iOSDev

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?

我知道有大量代码可以将 tableview 滚动到顶部,但是我想在点击顶部状态栏时执行此操作,就像在 Apple 的本机应用程序中一样。这可能吗?

回答by Mark Granoff

You get this for free, but you should check that the scrollsToTopattribute of your UITableViewis YES.

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

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

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

回答by Zane Claes

If you came from Google and need a complete checklist:

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

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

回答by Brandon Roberts

Using the information given in other answers, I added the following code to my UITableViewController get it to work:

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

- (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.

这会查看 UITableViewController 层次结构中的所有视图,并关闭所有拦截触摸事件的 UITextViews 上的 scrollsToTop。然后,确保 tableView 仍然会收到触摸。

You can mod this to iterate through other UITableViews / UIScrollViews / UITextViews that may be intercepting as well.

你可以修改它来遍历其他可能被拦截的 UITableViews/UIScrollViews/UITextViews。

Hope this helps!

希望这可以帮助!

回答by iOS Developer

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.
  1. 为要滚动到顶部的 tableview 设置 scrollsToTop = YES。
  2. 为所有其他 tableview 或 collection view 或 scrollview 设置 scrollsToTop = NO。
  3. 如果您的任何 tableview 单元格具有 collection view 。确保您也将集合视图的 scrollsToTop 设置为 NO。

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

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

回答by DennyLou

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.

我知道这是一个很旧的但希望这可以帮助。按照@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];
        }
    }
}

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.

这确实只是一个调试代码。一旦找到每个 UIScrollView 子类的 scrollsToTop 值,只需确保只有一个设置为 YES。

回答by George

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 的子类(通常是表格视图),它的 scrollsToTop 属性设置为 TRUE。可能您还有其他人,通常是您视图中的 UITextView 。只需将他们的 scrollsToTop 属性设置为 FALSE 就可以了。

回答by foskon

On UIScrollView header file:

在 UIScrollView 头文件上:

// 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 scrollsToTopproperty 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.

// 当用户点击状态栏时,最接近状态栏的触摸下方的滚动视图会滚动到顶部,但只有当它的scrollsToTop属性为YES时,它的delegate才不会从 返回NO shouldScrollViewScrollToTop,它也不是在顶部。// 在 iPhone 上,我们只有在有一个scrollsToTop== YES的屏幕滚动视图时才执行这个手势。如果找到多个,则不会滚动。

回答by Abo3atef

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

你应该做这样的事情 viewDidLoad

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

回答by Géza Mikló

There can be multiple UIScrollView descendantsloaded eg.: having a UIPageViewcontroller on each page containing UITableViews.

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

The scrollsToTopproperty is trueby default.

scrollsToTop属性是真正的默认。

So in addition to handling nested UIScrollViews' scrollsToTop property, you should do the following:

因此,除了处理嵌套的 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
}

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

这样只有一个顶级 UITableView 的 scrollsToTop 会被设置为 true。