How do I “hide” a UIRefreshControl?

前端 未结 12 957
醉梦人生
醉梦人生 2020-12-15 16:16

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 viewDid

12条回答
  •  粉色の甜心
    2020-12-15 17:02

    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.

提交回复
热议问题