0

I have added header with custom view to my TableView. It is used as a search bar, I set contentOffset so it is hidden in beginning.

Everything works as expected but I am wondering if there is any way to make it harder to pull down? (should be stickier) Now it opens with just regular scrolling which is too easy.

EDIT: ScrollViewDidScroll Code

if tableView.contentOffset.y < 80 && tableView.contentOffset.y > 40 {
            subscriptionsTableView.contentOffset.y += 0.23

            print("Content offset")
            print(tableView.contentOffset.y)
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
ZassX
  • 1,369
  • 2
  • 14
  • 35
  • Show off some images / code if you want people to be able to help you ! – Alexandre Beaudet Jul 21 '16 at 08:28
  • What is there to post code about? I explained everything, added custom view to header and it works. I just need some piece of code or an idea how to make stickier effect when pulling uitableview down when appearing tableview header. – ZassX Jul 21 '16 at 08:31
  • @ZassX did you find a solution? Can you share it? – Igor Kulman Jun 06 '17 at 13:42
  • @IgorKulman Uhh that was a long time ago and I don't have code anymore. I think I implemented it using pull to refresh with a custom view. http://www.appcoda.com/custom-pull-to-refresh/ – ZassX Jun 06 '17 at 14:03
  • 1
    @ZassX thanks, nearly works, just the height is not sufficient and it does not look like the height of the refresh control can be changed – Igor Kulman Jun 06 '17 at 14:46

2 Answers2

2

I had a similiar problem and solved in quite easily in the end. I set the table view top inset to the height of the header

tableView.contentInset.top = -1 * headerView.frame.size.height

This makes the header be shown "off screen", the user is now not being able to scroll to it. I added a code to change the top inset to 0, showing the header, when the user scrolls to the top over the header

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y < 0 {
        UIView.animate(withDuration: 0.25, animations: {
            self.tableView.contentInset.top = 0
        })
    } else if scrollView.contentOffset.y > headerView.frame.size.height {
        UIView.animate(withDuration: 0.25, animations: {
            self.searchBar.resignFirstResponder()
            self.tableView.contentInset.top = -1 * self.headerView.frame.size.height
        })
    }
}

I also set the top inset back when the user scrolls the table view.

Igor Kulman
  • 16,211
  • 10
  • 57
  • 118
1

My 2 cents: observe the contentOffset property of the table view (or implement the scrollViewDidScroll: method) and adjust the position of the view you want to stick.

Here is a reference to something that uses the same principle:

Community
  • 1
  • 1
Alessandro Orrù
  • 3,443
  • 20
  • 24
  • I was thinking in the same way. Already wrote a line of code (updated my question with it) but don't get the effect I want. I thought it would make scroll stickier / harder but actually sticks to top and you have to apply a faster swipe down to show it. Thank you for links by the way will look into! – ZassX Jul 21 '16 at 08:38
  • If by "stickier" you mean just that it scrolls slower, you probably just need to reduce the content offset by a ratio. I'm not sure I understand the exact behavior you'd like to achieve – Alessandro Orrù Jul 21 '16 at 08:49
  • I would like header to be hidden when view appears (already achieved that with content offset). When user pulls tableview down, header should appear. (just like UIRefreshControl). The problem is that it appears too easily - it is easy to pull it down, same speed as you were scrolling through tableview. I would like to give user a feeling that it is harder to pull it down - i don't know, like reduce scroll speed to half or something. To give user a feeling that it is on some kind of spring or something. I hope I explained it well enough to understand. – ZassX Jul 21 '16 at 09:22
  • After that view fully appears, should it be stick to the top? – Alessandro Orrù Jul 21 '16 at 09:27
  • No. If user scrolled down the tableview it would hide. – ZassX Jul 21 '16 at 09:34