문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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];

Try this:

[self.refreshControl removeFromSuperview];
self.refreshControl = nil;

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

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

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.

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.

To hide refresh control and avoid warning Just use

Objective C

[self.refreshControl removeFromSuperview];

Swift

self.refreshControl.removeFromSuperview()

I solved this problem by calling "yourRefreshControl".endEditing() inside the refresh function.

[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);
}

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];
}

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.

Tried using this solution from iOS_Mouse on Xcode 12. IOS 14.4.1 but it didn't work properly. Managed to tweak it to be this. (note that this is for my use case with UISegmentedControl. I have a different tables, of which only 1 I wanted to be able to use Pull To Refresh feature.

  var refreshControl = UIRefreshControl()

      @IBAction func vcLibFilterPressed(_ sender: UISegmentedControl) {
        removeRefreshControl()
        switch vcLibSegmentedControl.selectedSegmentIndex {
        
        case 0:        // Plan
          enableDisableSegmentedControl()
          vcLibTableView.reloadData()
          
          
        case 1:        // Tag
          vcLibTableView.reloadData()
          createRefreshControl()
    
        default:
          break
        }
      }


  func createRefreshControl() {
    refreshControl.addTarget(self, action: #selector(syncDropbox), for: .valueChanged)
    refreshControl.tintColor = UIColor.red
    refreshControl.attributedTitle = NSAttributedString(string: " ↓ Refresh ↓ ")
    tableView.refreshControl = refreshControl
  }

  func removeRefreshControl() {
    refreshControl.removeTarget(self, action: #selector(syncDropbox), for: .valueChanged)
    tableView.refreshControl = nil
  }
  
  
  @objc func syncDropbox(refreshControl: UIRefreshControl) {
    if vcLibSegmentedControl.selectedSegmentIndex == 1 {
      // DO Internet Stuffs

      // somewhere in your code you might need to call:
      refreshControl.endRefreshing()
    }
  }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top