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

发布于 2024-12-13 17:18:17 字数 593 浏览 0 评论 0原文

目前,UIActivityIndi​​cator 在屏幕上出现一两秒。我想将背景设置为灰色,因为它出现在屏幕上,但我不确定如何执行此操作...

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

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


}

任何帮助将不胜感激。

I currently have a UIActivityIndicator appearing 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...

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

放我走吧 2024-12-20 17:18:17

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

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

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];
遥远的绿洲 2024-12-20 17:18:17

您应该查看 SVProgressHUD

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

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

You should check out the SVProgressHUD

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

The SVProgressHUDMaskType has options to

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)
};`
是你 2024-12-20 17:18:17

尝试这个简单的方法

Its working well for me....

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

Try this simple method


Its working well for me....

- (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];
}
风渺 2024-12-20 17:18:17

实际上,我认为你可以做得更简单:)

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

你可以在情节提要中设置背景颜色,或者使用以下命令以编程方式进行设置:

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

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

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.

内心荒芜 2024-12-20 17:18:17

您可以将一个视图放入背景色为黑色、不透明度为 0.5 的窗口中。将其放入窗口中也会阻止导航控制器和选项卡栏控制器。

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.

单身情人 2024-12-20 17:18:17

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

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

当然,不要忘记:

#import <QuartzCore/QuartzCore.h>

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

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.

顾挽 2024-12-20 17:18:17

@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)

@SVMRAJESH Answer in 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)
千纸鹤带着心事 2024-12-20 17:18:17

从 Xcode 8.3 开始,有一种方法可以在情节提要中执行此操作:

  1. 将活动视图添加到场景中,最好添加到视图中对象列表的底部,这样它就会出现在所有内容的“顶部”,
  2. 使其覆盖通过添加约束来显示整个视图
  3. 单击属性检查器
  4. 启用“停止时隐藏”
  5. 设置“Alpha”> 0(0.3 较好)
  6. 将“背景”设置为深色(黑色效果很好)

这是屏幕截图。

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

  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)

Here's a screenshot.

陪你搞怪i 2024-12-20 17:18:17

Swift 3

设置背景颜色

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

创建和配置活动指示器

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

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

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()

Swift 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.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文