UIRefreshControl Stuck After Switching Tabs in UITabBarController UIRefreshControl Stuck After Switching Tabs in UITabBarController ios ios

UIRefreshControl Stuck After Switching Tabs in UITabBarController


I know this is incredibly late, but I figure better late than never.

Unfortunately, none of the given answers worked for me. The only thing that worked was this awful piece of code in viewWillAppear:

self.refreshControl = nil;UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];[refreshControl addTarget:self action:@selector(refreshFunction) forControlEvents:UIControlEventValueChanged];self.refreshControl = refreshControl;

Basically I recreate the UIRefreshControl every time the view is going to appear. It works, albeit while giving the following warning:

Attempting to change the refresh control while it is not idle is strongly discouraged and probably won't work properly.

I'm honestly surprised this is still an issue (still seeing this in iOS 9 now). Hopefully this may be helpful to some other people.


user2009606 was almost right, I fixed it by calling

[refreshControl endRefreshing];

On

viewWillAppear:

The refreshControl now behaves properly after switching tabs, independent of its state.


To sum up and provide a complete solution.

The problem:

If UIRefreshControl is animating during screen transition, it will stay visible but will not be animating. Video https://vid.me/Bkb7

Also, if pull to refresh animation was started but not completed, and then transition was performed, UIRefreshControl will be hidden but stuck. Video https://vid.me/Bkb7

Solution:

Start UIRefreshControl animation on viewWillAppear and end it on viewDidDisappear. Save the state of refresh process to know when to show UIRefreshControl.

Bellow is the whole solution that also handles the proper animation and simple pull to refresh animation.

Add to UITableView or subclass

Initialization:

 /// Refresh indicatorvar refreshControl = UIRefreshControl()/// Refresh statevar isRefreshing = false/// Refresh controll update funcion. Set to enable pull to refreshvar refreshControllUpdateFunction: (() -> ())?/// Prepeares UIRefreshControll. Call from initfunc initRefreshControl(){    addSubview(refreshControl)    refreshControl.addTarget(self, action: "refreshControllTarget", forControlEvents: .ValueChanged)    refreshControl.beginRefreshing()    isRefreshing = true}

Superview event handlers:

/// Call on viewWillApperfunc superviewWillApper(){    if isRefreshing && !refreshControl.refreshing{        startRefreshAnimation()    }}/// Call on viewDidDisapperfunc superviewDidDisappear(){    endRefreshAnimation(false, dataFetched: !isRefreshing)}

UIRefreshControl animation handler:

/// Presents animating UIRefreshControllfunc startRefreshAnimation(){    refreshControl.beginRefreshing()    contentOffset = CGPointMake(0, -refreshControl.bounds.size.height)    isRefreshing = true}/// Hides UIRefreshControll and saves state of refreshfunc endRefreshAnimation(wasEmpty: Bool, dataFetched: Bool){    refreshControl.endRefreshing()    isRefreshing = !dataFetched    if !wasEmpty{        setContentOffset(CGPointZero, animated: true)    }else{        setContentOffset(CGPointZero, animated: false)    }}

Add

table.isRefreshing = true

to begining of the refresh call.

Result video https://vid.me/LyuI