How do I “hide” a UIRefreshControl?

前端 未结 12 934
醉梦人生
醉梦人生 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 16:57

    I solved it this way:

    -(void)updateUIWithAuthState:(BOOL)isAuthenticated {
        self.loginButton.enabled = !isAuthenticated;
        self.loginButton.tintColor = isAuthenticated ? [UIColor clearColor] : nil;
    
        self.logoutButton.enabled = isAuthenticated;
        self.logoutButton.tintColor = isAuthenticated ? nil : [UIColor clearColor];
    
        self.tableView.userInteractionEnabled = isAuthenticated;
        self.data = nil;
        [self.tableView reloadData];
    }
    
    0 讨论(0)
  • 2020-12-15 16:58

    The best to implement UIRefreshControl is below.

     -(void)addRefreshControll{
        self.refreshControl=[[UIRefreshControl alloc] init];
        self.refreshControl.tintColor=[UIColor colorWithRed:0 green:183.0/255.0 blue:213/255.0 alpha:1.0];
        self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"Loading history..."];
        [self.refreshControl addTarget:self action:@selector(loadMoreChatFromCoreData) forControlEvents:UIControlEventValueChanged];
        self.tableView.refreshControl = self.refreshControl;
    }
    

    When there is no more record to load then remove refreshControl by below line

    self.tableView.refreshControl = nil;
    

    I have implemented same working fine.

    0 讨论(0)
  • 2020-12-15 17:02

    You have several ways to do this. I think the best way is to do a check in the viewDidLoad method with:

    if (condition){
     //attach refreshControl
    }
    

    if this isn't possible the best way is put this code where you want to hide the refresh (I think in viewWillAppear method in if condition)

    //End refresh control
    [self.refreshControl endRefreshing];
    //Remove refresh control to superview
    [self.refreshControl removeFromSuperview];
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 17:03

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

    0 讨论(0)
  • 2020-12-15 17:03

    I had bad experience when set tableView.refreshConrol = nil , because when I set it back to old refreshControl, it started animation only in a second, so it looked not good, so when I need to disable refreshControl I use:

    tableView.refreshControl?.endRefreshing()
    tableView.refreshControl?.alpha = 0
    

    when I need it back I use:

    tableView.refreshControl?.alpha = 1
    // and if I need to show refreshing indicator immediately I write:
    tableView.refreshControl?.beginRefreshing()
    

    P.S. Setting isHidden, isEnabled, isUserInteractionEnabled didn't help

    0 讨论(0)
提交回复
热议问题