Henny Lee
Henny Lee

Reputation: 3062

Reveal UISearchBar when UITableView is dragged from the first row

My search bar is at the top of the screen, hiding underneath the navigation bar. I'd like to show the search bar when the user is panning, only when the table view is at the top (at row 0). Can someone explain this?

I've tried using the pan gesture, but then the search bar will show every time I pan downwards.

func showSearchBar(recognizer: UIPanGestureRecognizer) {
    if recognizer.state == .Changed {
        let translation = recognizer.translationInView(view)

        if searchBarConstraint.constant < searchBar.bounds.height {
            searchBarConstraint.constant += translation.y
        }
    }
}

Upvotes: 1

Views: 97

Answers (1)

joern
joern

Reputation: 27620

As UITableView is a subclass of UIScrollView you can use its contentOffset to determine whether the table view is at the top or not:

let isAtTop = tableView.contentOffset.y == 0

Of course this only works it you have not set the default contentOffset to another point. Otherwise you would have to check for the y value of your default contentOffset.

Upvotes: 2

Related Questions