4

I have many views and for each views, many subviews. I tried to specify for each subviews

scrollsToTop = false

but my TableView doesn't scroll to top when I press the status bar. I suppose I missed some views...

SB

I know there is a similar post but it doesn't answer (UITableView scroll to top when tapping status bar at top of the screen)

So I decided to implement this function:

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)
        }
    }
}

But it doesn't work too. I don't know what I miss.

Community
  • 1
  • 1
cmii
  • 3,556
  • 8
  • 38
  • 69

2 Answers2

5

You may stuggle with 2 problems:

  1. You have more than one instance of UIScrollView. You should disable scrollsToTop (eg. self.collectionView?.scrollsToTop = false in viewDidLoad) for the parent UIScrollView or objects that inherit from it (like UITableView or UICollectionView).

From 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. If your hierarchy is highly customized method scrollViewShouldScrollToTop won't be called. You should check why does happen. Otherwise you will have to some dirty work: add some gestures and handle them.

Interesting links:

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:

Community
  • 1
  • 1
Szu
  • 2,254
  • 1
  • 21
  • 37
5

Ok I found why my tableview wouldn't scroll!

I called my checkForScrollViewInView() function only in the viewDidLoad of my table view's view controller. In order to disable scrollToTop for every child views.

But I forgot that I had another "active" view controller... In my app I have a left menu which opens to swipe. Calling checkForScrollViewInView() in the left menu's view controller solves my issue.

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)
        }
    }
}
cmii
  • 3,556
  • 8
  • 38
  • 69