ios 如何在顶部状态栏中显示加载指示器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/166712/
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-30 15:18:38  来源:igfitidea点击:

How to show the loading indicator in the top status bar

ioscocoa-touch

提问by rustyshelf

I have noticed that some apps like Safari and Mail show a loading indicator in the status bar (the bar at the very top of the phone) when they are accessing the network. Is there a way to do the same thing in SDK apps, or is this an Apple only thing?

我注意到某些应用程序(例如 Safari 和 Mail)在访问网络时会在状态栏(手机顶部的栏)中显示加载指示器。有没有办法在 SDK 应用程序中做同样的事情,或者这是苹果唯一的事情?

回答by Stephen Darlington

It's in UIApplication:

它在 UIApplication 中:

For Objective C:

对于目标 C:

Start:

开始:

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

End:

结尾:

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

For swift :

对于快速:

Start

开始

UIApplication.shared.isNetworkActivityIndicatorVisible = true

End

结尾

UIApplication.shared.isNetworkActivityIndicatorVisible = false

回答by Michael Waterfall

I've found the following macros pretty useful!

我发现以下宏非常有用!

#define ShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
#define HideNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO

So you can just call ShowNetworkActivityIndicator();or HideNetworkActivityIndicator();from within your app (as long as the header is included of course!).

所以你可以在你的应用程序中调用ShowNetworkActivityIndicator();或调用HideNetworkActivityIndicator();(当然,只要包含标题!)。

回答by Resh32

I wrote a singleton that solves the problem of multiple connections by keeping a counter of what is happening (to avoid removing the status when a connection returns but another one is still active):

我写了一个单例,它通过保持正在发生的事情的计数器来解决多个连接的问题(以避免在连接返回但另一个连接仍然活动时删除状态):

The header file:

头文件:

#import <Foundation/Foundation.h>

@interface RMActivityIndicator : NSObject

-(void)increaseActivity;
-(void)decreaseActivity;
-(void)noActivity;

+(RMActivityIndicator *)sharedManager;

@end

and implementation:

和实施:

#import "RMActivityIndicator.h"

@interface RMActivityIndicator ()

@property(nonatomic,assign) unsigned int activityCounter;

@end

@implementation RMActivityIndicator

- (id)init
{
    self = [super init];
    if (self) {
        self.activityCounter = 0;
    }
    return self;
}

    -(void)increaseActivity{
        @synchronized(self) {
             self.activityCounter++;
        }
        [self updateActivity];
    }
-(void)decreaseActivity{
    @synchronized(self) {
           if (self.activityCounter>0) self.activityCounter--;
    }
    [self updateActivity];
}
-(void)noActivity{
    self.activityCounter = 0;
    [self updateActivity];
}

-(void)updateActivity{
    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = (self.activityCounter>0);
}

#pragma mark -
#pragma mark Singleton instance

+(RMActivityIndicator *)sharedManager {
    static dispatch_once_t pred;
    static RMActivityIndicator *shared = nil;

    dispatch_once(&pred, ^{
        shared = [[RMActivityIndicator alloc] init];
    });
    return shared;
}

@end

Example:

例子:

    [[RMActivityIndicator sharedManager]increaseActivity];
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:self.networkReceiveProcessQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        [[RMActivityIndicator sharedManager]decreaseActivity];
    }

回答by asish

A single line code to do that:

一个单行代码来做到这一点:

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

回答by Babu Lal

You need to take care of hiding the activity indicator also once your network call is done.

网络调用完成后,您还需要注意隐藏活动指示器。

If you use AFNetworking, then you don't need to do much.

如果您使用AFNetworking,那么您不需要做太多事情。

Do following changes in AppDelegateClass:

AppDelegate类中进行以下更改:

  1. Import AFNetworking/AFNetworkActivityIndicatorManager.h

  2. Put this in didFinishLaunchingWithOptions:

  1. 进口 AFNetworking/AFNetworkActivityIndicatorManager.h

  2. 把这个放进去 didFinishLaunchingWithOptions:

[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]

[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]

回答by M Reza Farahani

The status bar network activity indicator was deprecated in iOS 13.

状态栏网络活动指示器在iOS 13中已弃用。

Using UIApplication.shared.isNetworkActivityIndicatorVisible = truewill not work anymore.

使用UIApplication.shared.isNetworkActivityIndicatorVisible = true将不再起作用。

The deprecation message says:

弃用消息说:

Provide a custom network activity UI in your app if desired.

如果需要,请在您的应用中提供自定义网络活动 UI。

回答by Sev

It might also be helpful to make sure you are running it on the main thread as it is UI related.

确保您在主线程上运行它也可能会有所帮助,因为它与 UI 相关。

dispatch_async(dispatch_get_main_queue(), ^{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
});

回答by erickva

As many have said, there is no network activity indicator for the iPhone X and probably for the other new iPhones with the notch.

正如许多人所说,iPhone X 没有网络活动指示器,其他带有缺口的新 iPhone 也可能没有网络活动指示器。

I came across this incredible library written by Ortwin Gentz, FutureTap: https://github.com/futuretap/FTLinearActivityIndicator

我遇到了这个由 Ortwin Gentz 编写的令人难以置信的库,FutureTap:https: //github.com/futuretap/FTLinearActivityIndi​​cator

It puts the indicator right back where it was when the iPhone X was initially released, many would remember the Knight Rider type of indicator.

它让指示器回到 iPhone X 最初发布时的位置,许多人会记得 Knight Rider 类型的指示器。

This library is available for Swift 4.2, so you will need to change the Swift Language settings, as described here: Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'

此库可用于 Swift 4.2,因此您需要更改 Swift 语言设置,如下所述: Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'