在 iOS 中加载 UIView 时如何为 UIActivityIndicator 设置灰色背景
目前,UIActivityIndicator
在屏幕上出现一两秒。我想将背景设置为灰色,因为它出现在屏幕上,但我不确定如何执行此操作...
这是到目前为止我初始化指示器的方式。
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不需要单独的视图。这是一个简单的机制:
No need for a separate view. Here's a simple mechanism:
您应该查看 SVProgressHUD
它具有用于遮盖背景的选项,并且使用起来非常简单。
SVProgressHUDMaskType 有以下选项
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
尝试这个简单的方法
Its working well for me....
Try this simple method
Its working well for me....
实际上,我认为你可以做得更简单:)
你可以在情节提要中设置背景颜色,或者使用以下命令以编程方式进行设置:
确保在 Xcode 的属性检查器中选中“停止时隐藏”,并选择活动指示器。
Actually I think you can do it more simply :)
You can set the background color in the storyboard or do it programmatically using
Make sure to check "Hides When Stopped" in the attributes inspector in Xcode with the activity indicator selected.
您可以将一个视图放入背景色为黑色、不透明度为 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.
更简单的是,您可以将图层背景颜色设置为半透明黑色(或任何其他颜色):
当然,不要忘记:
这样微调器也将保持不透明。
Even simpler, you can set layer background colour to semi-transparent black (or any other colour):
Of course, don't forget to:
This way also the spinner will remain opaque.
@SVMRAJESH 在 Swift 2.0 中回答
@SVMRAJESH Answer in Swift 2.0
从 Xcode 8.3 开始,有一种方法可以在情节提要中执行此操作:
这是屏幕截图。
As of Xcode 8.3, there's a way to do this in storyboard:
Here's a screenshot.
Swift 3
设置背景颜色
创建和配置活动指示器
在 viewDidLoad 中,调用下面的方法来创建和配置活动指示器,也不要忘记添加到视图中(您希望在其中显示活动指示器)通过在 viewDidLoad 中调用
view.addSubview(activityIndicatorView)
。还对其设置约束。Swift 3
To set backgroundColor
To create and configure 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.