在 iOS 中的 UIView 上加载时如何为 UIActivityIndi​​cator 设置灰色背景

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

How to set grey color background for UIActivityIndicator when loading on UIView in iOS

iphoneiosuiactivityindicatorviewviewdidload

提问by C.Johns

I currently have a UIActivityIndicatorappearing on screen for a second or two. I would like to set grey out the background as this appears on screen but I am not sure how to do this...

我目前有一个UIActivityIndicator出现在屏幕上一两秒钟。我想将背景设置为灰色,因为它出现在屏幕上,但我不知道如何做到这一点......

Here's how I have initialized the indicator so far.

到目前为止,这是我初始化指标的方式。

- (void)viewDidLoad
{
//...

    activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [activity setCenter:CGPointMake(160.0f, 208.0f)];
    [activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];

    [self.tableView addSubview:activity];

    [activity startAnimating];

    [activity performSelector:@selector(stopAnimating) withObject:nil afterDelay:1.0];


}

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by mahboudz

No need for a separate view. Here's a simple mechanism:

不需要单独的视图。这是一个简单的机制:

    UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    self.activityIndicatorView = activity;
    // make the area larger
    [activity setFrame:self.view.frame;
    // set a background color
    [activity.layer setBackgroundColor:[[UIColor colorWithWhite: 0.0 alpha:0.30] CGColor]];
    CGPoint center = self.view.center;
    activity.center = center;
    [activity release];

回答by Christian Schlensker

You should check out the SVProgressHUD

您应该查看SVProgressHUD

It has options for masking the background and is dead simple to work with.

它具有用于掩盖背景的选项,并且使用起来非常简单。

The SVProgressHUDMaskType has options to

SVProgressHUDMaskType 可以选择

enum {
 SVProgressHUDMaskTypeNone = 1, // allow user interactions, don't dim background UI (default)
SVProgressHUDMaskTypeClear, // disable user interactions, don't dim background UI
SVProgressHUDMaskTypeBlack, // disable user interactions, dim background UI with 50% translucent black
SVProgressHUDMaskTypeGradient // disable user interactions, dim background UI with translucent radial gradient (a-la-alertView)
};`

回答by Rajesh Loganathan

Try this simple method

试试这个简单的方法

它对我来说效果很好......

- (void)viewDidLoad
{
    UIActivityIndicatorView *activityIndicator= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    activityIndicator.layer.cornerRadius = 05;
    activityIndicator.opaque = NO;
    activityIndicator.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
    activityIndicator.center = self.view.center;
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [activityIndicator setColor:[UIColor colorWithRed:0.6 green:0.8 blue:1.0 alpha:1.0]];
    [self.view addSubview: activityIndicator];
}

回答by thepauljones

Actually I think you can do it more simply :)

其实我认为你可以更简单地做到这一点:)

[_activityIndicator setBounds:self.view.frame];
[_activityIndicator setCenter:self.view.center];
[_activityIndicator setAlpha:0.5f];
[_activityIndicator startAnimating];

You can set the background color in the storyboard or do it programmatically using

您可以在情节提要中设置背景颜色或使用编程方式进行设置

    UIColor *activityBackgroundColor = [UIColor colorWithRed:0.6 green:0.8 blue:1.0 alpha:1.0];
[_activityIndicator setColor:activityBackgroundColor];

Make sure to check "Hides When Stopped" in the attributes inspector in Xcode with the activity indicator selected.

确保在选中活动指示器的情况下在 Xcode 的属性检查器中选中“停止时隐藏”。

回答by Micah Hainline

You could put a view in to the window that has a background color of black with opacity of 0.5. Putting it into the window will block navigation controllers and tab bar controllers as well.

您可以在窗口中放置一个背景颜色为黑色、不透明度为 0.5 的视图。将它放入窗口也会阻止导航控制器和标签栏控制器。

回答by msrdjan

Even simpler, you can set layer background colour to semi-transparent black (or any other colour):

更简单的是,您可以将图层背景颜色设置为半透明黑色(或任何其他颜色):

self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activityIndicatorView.layer.backgroundColor = [[UIColor colorWithWhite:0.0f alpha:0.5f] CGColor];
self.activityIndicatorView.hidesWhenStopped = YES;
self.activityIndicatorView.frame = self.view.bounds;
[self.view addSubview:self.activityIndicatorView];

Of course, don't forget to:

当然,不要忘记:

#import <QuartzCore/QuartzCore.h>

This way also the spinner will remain opaque.

这样,微调器也将保持不透明。

回答by IRFAN SHEIKH

@SVMRAJESH Answer in Swift 2.0

@SVMRAJESH Swift 2.0 中的答案

 let loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
    loadingIndicator.layer.cornerRadius = 05

    loadingIndicator.opaque = false
    loadingIndicator.backgroundColor = UIColor(white: 0.0, alpha: 0.6)
    loadingIndicator.center = self.view.center;
    loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
    loadingIndicator.color = UIColor(red:0.6,green:0.8,blue:1.0,alpha:1.0)
    loadingIndicator.startAnimating()
    self.view.addSubview(loadingIndicator)

回答by Chris Leung

As of Xcode 8.3, there's a way to do this in storyboard:

从 Xcode 8.3 开始,有一种方法可以在故事板中执行此操作:

  1. Add the Activity View into your scene, preferably to the bottom of the list of objects in your view, so that it'll appear "on top" of everything
  2. Make it cover the entire view by adding constraints
  3. Click on the Attributes Inspector
  4. Enable "Hides When Stopped"
  5. Set "Alpha" > 0 (0.3 is good)
  6. Set "Background" to a dark color (black works well)
  1. 将活动视图添加到您的场景中,最好添加到您视图中对象列表的底部,以便它会出现在所有内容的“顶部”
  2. 通过添加约束使其覆盖整个视图
  3. 单击属性检查器
  4. 启用“停止时隐藏”
  5. 设置“Alpha”> 0(0.3 很好)
  6. 将“背景”设置为深色(黑色效果很好)

Here's a screenshot.

这是一个屏幕截图

回答by user44776

Swift 3

斯威夫特 3

To set backgroundColor

设置背景颜色

activityIndicatorView.backgroundColor = UIColor.gray // Set Activity indicator backgroundColor 

To create and configure activity indicator

创建和配置活动指示器

var activityIndicatorView   : UIActivityIndicatorView! // Declare variable to hold activity indicator

In viewDidLoad, Call the below method to create and configure the activity indicator, also don't forget to add to the view (where you would like to show the activity indicator) by calling view.addSubview(activityIndicatorView)in viewDidLoad. Also set constraints to it.

在 viewDidLoad 中,调用下面的方法来创建和配置活动指示器,也不要忘记通过调用view.addSubview(activityIndicatorView)viewDidLoad添加到视图(您希望显示活动指示器的位置)。还要对其设置约束。

func createAndconfigureActivityIndicatorView() {

        activityIndicatorView  = UIActivityIndicatorView(activityIndicatorStyle: .white) 

        activityIndicatorView.backgroundColor = UIColor.gray // Set Activity indicator backgroundColor 

        activityIndicatorView.startAnimating() // To start animation

        activityIndicatorView.hidesWhenStopped = true // Setting this true means, when you stop the activity indicator, its automatically hidden (see below)
}

// Stops and Hides Activity Indicator
activityIndicatorView.stopAnimating()