How Do I Shorten the Pull Distance on UIRefreshControl to Activate the Pull to Refresh Action?

10,821

Solution 1

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

Solution 2

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

Solution 3

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
    }
}
Share:
10,821

Related videos on Youtube

Danchez
Author by

Danchez

Young adult and aspiring developer in all things object-oriented hoping to become very proficient in the art of programming in any development environment. Experienced in iOS development and working with Objective-C and Swift. Also experienced in Agile working environments and apply development practices/methodologies like pair programming, Test Driven Development (TDD), Behavior Driven Development (BDD) and working with iOS BDD test frameworks like Cedar.

Updated on September 16, 2022

Comments

  • Danchez
    Danchez over 1 year

    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
    Danchez over 10 years
    Does 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.
  • staticVoidMan
    staticVoidMan over 10 years
    @DanielSanchez: I haven't had the need to modify it yet. anyways, this may help: github.com/Sephiroth87/ODRefreshControl/pull/…
  • staticVoidMan
    staticVoidMan over 10 years
    @DanielSanchez aah, in the ODRefreshControl.m modify the #define kMaxDistance 53 parameter. i guess that'll help
  • Danchez
    Danchez over 10 years
    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 and modified that #define kMaxDistance 53 constant to get my desired output. Woohoo!
  • staticVoidMan
    staticVoidMan over 10 years
    @DanielSanchez : no probs :) i have edited my answer to include this important bit of information
  • Fattie
    Fattie over 7 years
    This answer would seem to be wrong now guys. You can just call .beginRefreshing() as Ashkan explains.