可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
Try setting your table view controller's refreshControl
property to nil.
回答2:
Try this:
[self.refreshControl removeFromSuperview];
self.refreshControl = nil;
回答3:
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];
回答4:
There’s a very simple solution you can try: [self.refreshControl removeFromSuperview];
回答5:
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
回答6:
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.
回答7:
To hide refresh control and avoid warning Just use
Objective C
[self.refreshControl removeFromSuperview];
Swift
self.refreshControl.removeFromSuperview()
回答8:
I solved this problem by calling "yourRefreshControl".endEditing()
inside the refresh function.
回答9:
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.
回答10:
[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);
}
回答11:
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];
}
回答12:
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.