Search code examples
iosobjective-cuitableviewuirefreshcontrol

Pull to refresh default refresh level change


I am using UIRefreshControl. I want to change its refresh action on pull down a tableview. When tableview slight pull down I need to refresh because my tableview has small height and I can't pull down enough to call refresh method.

How can I change it?

I simply added UIRefreshControl as below:

self.refreshControl = [[UIRefreshControl alloc]init];
_refreshControl.tintColor = [UIColor redColor];
[self.tweetTable addSubview:self.refreshControl];
[self.refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];

Solution

  • Since I do not think you can change the behaviour of how much distance you have to travel to initiate the refresh I would just trigger it manually when user scrolled enough, something like:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard !refreshControl.isRefreshing else {
            return//do nothing if we are already refreshing
        }
    
        //set your threshold to whatever feels ok (I used -30 here)
        if scrollView.contentOffset.y < -30 {
            refreshTable()
            refreshControl.beginRefreshing()
        }
    }
    

    You also might have to play a bit with offsetting table view properly when refresh is active so that the UIActivityIndicator is above your cells, and then adjust it again when you finish refreshing. Note you will have to call refreshControl.endRefreshing() in refreshTable() method once API calls are completed or whatever you are doing there...