ios 如何“隐藏” UIRefreshControl?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19480424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:27:04  来源:igfitidea点击:

How do I "hide" a UIRefreshControl?

iosobjective-cuirefreshcontrol

提问by Doug Smith

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.

有时,我的表视图不会连接到要刷新的服务,在这种情况下,我不希望 UIRefreshControl 出现。

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

在 viewDidLoad 中添加它后,我尝试在某些情况下使用setEnabled:和隐藏它,setHidden:但似乎都不起作用。

回答by Jonathan Arbogast

Try setting your table view controller's refreshControlproperty to nil.

尝试将您的表视图控制器的refreshControl属性设置为 nil。

回答by Sviatoslav Yakymiv

Try this:

尝试这个:

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

回答by Serluca

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

您有几种方法可以做到这一点。我认为最好的方法是检查 viewDidLoad 方法:

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)

如果这不可能,最好的方法是将此代码放在要隐藏刷新的位置(我认为在 if 条件下的 viewWillAppear 方法中)

//End refresh control
[self.refreshControl endRefreshing];
//Remove refresh control to superview
[self.refreshControl removeFromSuperview];

回答by jenish

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

您可以尝试一个非常简单的解决方案: [self.refreshControl removeFromSuperview];

回答by Paul T.

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 refreshControlI use:

我在 settableView.refreshConrol = nil时体验不好,因为当我将它设置回 old 时refreshControl,它只在一秒钟内启动动画,所以看起来不太好,所以当我需要禁用时,refreshControl我使用:

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, isUserInteractionEnableddidn't help

PS设置isHidden, isEnabled, isUserInteractionEnabled没有帮助

回答by Eskil Olsen

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

我通过调用"yourRefreshControl".endEditing()refresh 函数解决了这个问题。

回答by priwiljay

To hide refresh control and avoid warning Just use

隐藏刷新控制并避免警告只需使用

Objective C

目标 C

[self.refreshControl removeFromSuperview];

[self.refreshControl removeFromSuperview];

Swift

迅速

self.refreshControl.removeFromSuperview()

回答by iOS_Mouse

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

斯威夫特 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.

当我想要创建控件时,我调用 createRefreshControl(),当我想要删除它时调用 removeRefreshControl。

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.

我必须删除最初添加到刷新控件的相同目标,否则它会在实际删除之前刷新一次。

回答by Arun_

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 setEnabled:NO,因此您必须将其从它的超级视图中删除。我尝试了使用 Apple 提供的 Reachability 类的示例。

要添加 UIRefreshControl,您可以使用:

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.

您可以通过使用 bool 标志来检查连接来实现,这里我提供了这个示例,使用 Apple 的可达性类来检查我的连接。

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.

希望这对你有用。

回答by Ilya Stroganov

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