ios - 通过点击状态栏滚动到顶部 TableView 不起作用(有很多 subview )SWIFT

标签 ios swift uitableview scroll statusbar

我有很多 View ,每个 View 都有很多 subview 。 我试图为每个 subview 指定

scrollsToTop = false

但是当我按下状态栏时,我的 TableView 没有滚动到顶部。 我想我错过了一些观点......

SB

我知道有一个类似的帖子,但它没有回答(UITableView scroll to top when tapping status bar at top of the screen)

所以我决定实现这个功能:

override func viewDidLoad() {   

    super.viewDidLoad()

    checkForScrollViewInView(self.view)

    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.tableView.scrollsToTop = true
}

func checkForScrollViewInView(view:UIView) {
    for subview in view.subviews as [UIView] {

        if subview.isKindOfClass(UITextView) {
            (subview as! UITextView).scrollsToTop = false
        }

        if subview.isKindOfClass(UIScrollView) {
            (subview as! UIScrollView).scrollsToTop = false
        }

        if subview.isKindOfClass(UITableView) {
            (subview as! UITableView).scrollsToTop = false
        }

        if (subview.subviews.count > 0) {
            self.checkForScrollViewInView(subview)
        }
    }
}

但是也行不通。我不知道我错过了什么。

最佳答案

您可能会遇到两个问题:

  1. 您有多个 UIScrollView 实例。您应该为父级 UIScrollView 禁用 scrollsToTop(例如 self.collectionView?.scrollsToTop = false in viewDidLoad)或继承自它的对象(如 UITableViewUICollectionView)。

来自 Apple scrollViewShouldScrollToTop(_:)

If the delegate doesn’t implement this method, true is assumed. For the scroll-to-top gesture (a tap on the status bar) to be effective, the scrollsToTop property of the UIScrollView must be set to YES.

  1. 如果您的层次结构是高度自定义的,则不会调用方法 scrollViewShouldScrollToTop。你应该检查为什么会发生。否则你将不得不做一些肮脏的工作:添加一些手势并处理它们。

有趣的链接:

Scroll to top of UITableView by tapping status bar

Why doesn't UIScrollView/UITableview respond to taps on the status bar, and scroll to the top?

https://developer.apple.com/library/prerelease/ios//documentation/UIKit/Reference/UIScrollViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIScrollViewDelegate/scrollViewShouldScrollToTop :

关于ios - 通过点击状态栏滚动到顶部 TableView 不起作用(有很多 subview )SWIFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33523220/

相关文章:

ios - 在表格 View 单元格上无法访问detailTextLabel和imageView

ios - 如何知道当前正在显示 UITableView 的哪些单元格(行和部分)?

ios - iPad性能问题

macos - 如何以编程方式将 NSWindow 设置为全屏?

swift - 代码 8 : How to pass data to a view controller without using a segue or storyboard?

iOS Firebase - 扇出时 -'InvalidPathValidation' ,原因 : '(child:) Must be a non-empty string and not contain ' .' ' #' ' $' ' [' or ' ]''

ios - 从 didSelectRow 方法中以编程方式推送新的 View Controller

ios - 为什么将UIViewController释放在主线程上?

ios - UIViewPropertyAnimator 不会停止在 stopAnimation 上

ios - 方法中返回 Json 对象,无法将 NSDictionary 转换为 Void