UIRefreshControl Stuck After Switching Tabs in UIT

2019-01-17 20:28发布

I have a UITableViewController as the root view controller in a UINavigationController, which is in turn one of the view controllers in a UITabBarController. All hooked up in Storyboard.

I've configured the UIRefreshControl for my table in Storyboard as well, and normally it looks like it should when pulling:

Normal UIRefreshControl

However, if I switch between my other tabs once or twice, it looks like this:

Buggy UIRefreshControl

It's not spinning or anything, just stuck "full", and it stays that way until I pull fully and trigger a refresh.

Any ideas or suggestions appreciate.

16条回答
冷血范
2楼-- · 2019-01-17 20:48

Nicest fix in Swift:

override func viewWillAppear(_ animated: Bool) {
  super.viewWillAppear(animated)

  //fixes bug with refreshControl freezing while switching tabs
  if tableView.contentOffset.y < 0 {
    tableView.contentOffset = .zero
  }
}
查看更多
迷人小祖宗
3楼-- · 2019-01-17 20:49

The answers in this question solved my problem

basically stopping and starting the spinner again on viewWillAppear

查看更多
Fickle 薄情
4楼-- · 2019-01-17 20:53

I have just seen the same thing. What I ended up doing is endRefreshing on viewWillDisappear and that at least eliminates this problem. However, coming back to the tab I expect it to start spinning again as I restart the refresh process and and call beginRefreshing again, but it does not. It does in the case when I drill down navigation and then come back though.

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-17 20:54

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.

查看更多
地球回转人心会变
6楼-- · 2019-01-17 20:55

Hope I can help solving this issue finally :

There is an issue using UITableView with a Pull-To-Refresh control. This can be EASILY solved, by adding a UITableViewController.

UITableViewController *tableController = [[UITableViewController alloc] init];
    tableController.automaticallyAdjustsScrollViewInsets = NO;
    [tableController willMoveToParentViewController:self];
    [self addChildViewController:tableController];
    tableController.tableView = self.containerTableView;
    tableController.refreshControl = self.refreshControl;

This way you just wrap your UITableView inside a controller for which the [self.refreshControl endRefreshing] will work completely fine inside viewWillAppear or viewWillDisappear. In some cases you might need to call it in both.

Hope this helps! :)

查看更多
闹够了就滚
7楼-- · 2019-01-17 20:59

You can try

refreshControl.beginRefreshing()
refreshControl.endRefreshing()

in viewWillAppear, and this should work.

查看更多
登录 后发表回答