How do I “hide” a UIRefreshControl?

2020-02-09 06:14发布

Occasionally my table view won't be connected to a service to refresh, and in that case, I don't want the UIRefreshControl to be present.

After I add it in viewDidLoad, I've tried hiding it under certain circumstances with setEnabled: and setHidden: but neither seems to work.

12条回答
迷人小祖宗
2楼-- · 2020-02-09 06:55
[refreshControl setTintColor:[UIColor clearColor]];

also you can do something like this:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y < 0)
        scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
}
查看更多
太酷不给撩
3楼-- · 2020-02-09 06:56

To hide refresh control and avoid warning Just use

Objective C

[self.refreshControl removeFromSuperview];

Swift

self.refreshControl.removeFromSuperview()
查看更多
对你真心纯属浪费
4楼-- · 2020-02-09 06:56

An old question, but I was looking for an answer and nothing worked exactly like I wanted.

This is what worked for me:

Swift 4

func createRefreshControl() {
    refreshControl = UIRefreshControl()
    refreshControl?.addTarget(self, action: #selector(self.myTableRefreshFunction), for: UIControlEvents.valueChanged)
    refreshControl?.tintColor = UIColor.white
    refreshControl?.endRefreshing()
}

func removeRefreshControl() {
    refreshControl?.removeTarget(self, action: #selector(self.myTableRefreshFunction), for: UIControlEvents.valueChanged)
    refreshControl = nil
}

I call createRefreshControl() when I want the control created, and removeRefreshControl when I want it removed.

I had to remove the same target I initially added to the refresh control, otherwise it would refresh one time before it was actually removed.

查看更多
混吃等死
5楼-- · 2020-02-09 07:08

There’s a very simple solution you can try: [self.refreshControl removeFromSuperview];

查看更多
淡お忘
6楼-- · 2020-02-09 07:10

Try setting your table view controller's refreshControl property to nil.

查看更多
ゆ 、 Hurt°
7楼-- · 2020-02-09 07:10

You can not remove the UIRefreshControl using setEnabled:NO,so to this you have to remove it from it's superview.I have tried a sample using Reachability class provided by Apple.

To add UIRefreshControl you can use this:

UIRefreshControl *refContr=[[UIRefreshControl alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        [refContr setTintColor:[UIColor blueColor]];
        [refContr setBackgroundColor:[UIColor greenColor]];

    [self.view addSubview:refContr];
    [refContr setAutoresizingMask:(UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin)];        
    [refContr addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];

Then Implemented reachability class notification :

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

You can do it by using bool flag to check the connectivity ,Here i'm providing this example using reachability class by apple to check my connectivity.

switch (netStatus)
{
    case NotReachable:        {

         for (UIRefreshControl *subView in [myView subviews]) {
             if ([subview isKindOfClass:[UIRefreshControl class]]) {
                 [subView removeFromSuperview];
             }
         }
          //or you could use [UIRefreshControl setHidden:YES];

          connectionRequired = YES;
          break;
    }

    case ReachableViaWiFi:        {
         for (UIRefreshControl *subView in [myView subviews]) {
             if ([subview isKindOfClass:[UIRefreshControl class]]) {
                 [subview removeFromSuperview];
             }else{
               [self.view addSubview:refContr];
         }
         //or you could use [UIRefreshControl setHidden:NO];
        break;
    }
} 

Hope this will work for you.

查看更多
登录 后发表回答