18

Hey StackOverflow People,

I've been trying to figure out this question for some time now but to no avail and I need some help. I have a UITableView close to the bottom of my app and there's not enough screen distance for the user to engage the refresh. Does anybody know how I can shorten the distance it takes to activate the pull to refresh action on a UIRefreshControl within a UITableView and UIWebView?

Thanks in advance everyone!

Danchez
  • 1,266
  • 2
  • 13
  • 28

4 Answers4

37

you can still use refreshControl but with some modifications!

add these code to your viewController:

var canRefresh = true

override func scrollViewDidScroll(scrollView: UIScrollView) {

    if scrollView.contentOffset.y < -100 { //change 100 to whatever you want

        if canRefresh && !self.refreshControl.refreshing {

            self.canRefresh = false
            self.refreshControl.beginRefreshing()

            self.refresh() // your viewController refresh function
        }
    }else if scrollView.contentOffset.y >= 0 { 

        self.canRefresh = true
    }
}

and as usual in the end of your refresh logic in self.refresh() function add :

   self.refreshControl.endRefreshing()
Ashkan Ghodrat
  • 3,162
  • 2
  • 32
  • 36
11

As per the Apple Docs, I don't see any way to modify UIRefreshControl parameters.
link: https://developer.apple.com/library/ios/documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html

Use a third-party component like ODRefreshControl (to customize the scroll-distance in order to activate the refresh, modify the #define kMaxDistance constant).

or...

Don't use the UIRefreshControl and instead implement your own logic in the -scrollViewDidScroll method like:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height) {
        //refresh logic
    }
}
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
  • Does [ODRefreshControl](https://github.com/Sephiroth87/ODRefreshControl) have modifiable parameters that affect the pull distance? I was looking at it but didn't see anything that did. Or I could just be blind and have a terrible case of tunnel vision today. – Danchez Nov 26 '13 at 18:27
  • 1
    @DanielSanchez: I haven't had the need to modify it yet. anyways, this may help: https://github.com/Sephiroth87/ODRefreshControl/pull/9#issuecomment-17048903 – staticVoidMan Nov 26 '13 at 18:51
  • 2
    @DanielSanchez aah, in the `ODRefreshControl.m` modify the `#define kMaxDistance 53` parameter. i guess that'll help – staticVoidMan Nov 26 '13 at 18:55
  • 2
    You sir are a genius! Thanks a lot! I believe this is the solution for anyone trying to do the same thing I'm doing. I just went with [ODRefreshControl](https://github.com/Sephiroth87/ODRefreshControl) and modified that `#define kMaxDistance 53` constant to get my desired output. Woohoo! – Danchez Nov 26 '13 at 20:53
  • @DanielSanchez : no probs :) i have edited my answer to include this _important_ bit of information – staticVoidMan Nov 28 '13 at 14:38
  • 1
    This answer would seem to be wrong now guys. You can just call .beginRefreshing() as Ashkan explains. – Fattie Dec 19 '16 at 21:58
1

For Swift 3.2 and above :

var canRefresh = true

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y < -100 {
        if canRefresh && !self.refreshControl.isRefreshing {
            self.canRefresh = false
            self.refreshControl.beginRefreshing()
            self.handleRefresh()
        }
    } else if scrollView.contentOffset.y >= 0 {
        self.canRefresh = true
    }
}
Ankit Kumar Gupta
  • 3,994
  • 4
  • 31
  • 54
0

you can make it right with some modification

lazy var refreshControl: UIRefreshControl = {
        let refreshControl = UIRefreshControl()
        refreshControl.tintColor = UIColor.red
        return refreshControl
    }()

//refreshControl.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)

every PullToRefresh must have couple lines of code like this, that handleRefresh function, do whatever you need to refresh the page.

you just need to comment out addTarget line and add this function to your code ```

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.y < -80 { //change 80 to whatever you want
            if  !self.refreshControl.isRefreshing {
                handleRefresh()
            }
        }
    }

I wrote this code with the help of Ashkan Ghodrat's answer

Hamish
  • 1,685
  • 22
  • 37