ios 将活动指示器颜色更改为黑色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29698272/
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
Changing activity indicator color to black
提问by Peter Pik
I'm trying to change my activity indicator to a custom color like black. However it does not seem to apply at all and has the standard white color still.
我正在尝试将我的活动指示器更改为黑色等自定义颜色。然而,它似乎根本不适用并且仍然具有标准的白色。
What i've done is creating a UIView and and added a activity indicator as a subView and then added this to the tableFooterView.
我所做的是创建一个 UIView 并添加一个活动指示器作为子视图,然后将其添加到 tableFooterView。
How can I change the activity indicator color?
如何更改活动指示器颜色?
ViewDidLoad:
ViewDidLoad:
let footerView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, 40))
footerView.backgroundColor = UIColor(rgba: "#f6f7f9")
var actInd: UIActivityIndicatorView = UIActivityIndicatorView()
actInd.color = UIColor.blackColor()
actInd.frame = CGRectMake(self.view.frame.size.width/2-10, 0.0, 20.0, 20.0);
actInd.activityIndicatorViewStyle =
UIActivityIndicatorViewStyle.WhiteLarge
footerView.addSubview(actInd)
actInd.startAnimating()
self.tableVIew.tableFooterView = footerView
回答by Catalina T.
You need to set the color after you set the activity indicator style. It seems that setting the activityIndicatorViewStyleresets the color for the activityIndicator
设置活动指示器样式后,您需要设置颜色。似乎设置activityIndicatorViewStyle重置了颜色activityIndicator
So just do this:
所以只需这样做:
// ....
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
activityIndicator.color = .black
footerView.addSubview(activityIndicator)
activityIndicator.startAnimating()
self.tableView.tableFooterView = footerView
This should work just fine.
这应该工作得很好。
回答by Nikola Lajic
The documentation states
该文件指出
If you set a color for an activity indicator, it overridesthe color provided by the activityIndicatorViewStyle property.
如果您为活动指示器设置颜色,它将覆盖由 activityIndicatorViewStyle 属性提供的颜色。
So you have to set the activityIndicatorViewStylefirst and then set the color.
所以你必须先设置activityIndicatorViewStyle然后再设置颜色。
Correct:
正确的:
actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
actInd.color = UIColor.blackColor()
Wrong:
错误的:
actInd.color = UIColor.blackColor()
actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
回答by CodeBender
Here is an extension for Swift 4 that makes it easy to color the indicator.
这是 Swift 4 的一个扩展,它可以轻松地为指示器着色。
import UIKit
extension UIActivityIndicatorView {
func assignColor(_ color: UIColor) {
activityIndicatorViewStyle = .whiteLarge
self.color = color
}
}
You call it like this:
你这样称呼它:
activityIndicator.assignColor(.black)
Here is this code in action, with a random timer to change colors:

回答by sputn1k
Swift 3 version
斯威夫特 3 版本
In main class ViewControlleradd activityIndicator:
在主类中ViewController添加activityIndicator:
class ViewController: UIViewController, WKNavigationDelegate {
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
Then in viewDidLoad():
然后在viewDidLoad():
override func viewDidLoad() {
super.viewDidLoad()
activityIndicator.color = UIColor.black
activityIndicator.center = CGPoint(x: view.bounds.size.width/2, y: view.bounds.size.height/2)
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
To remove activityIndicator:
删除活动指示器:
activityIndicator.stopAnimating()
回答by Akaino
You should change your color after you set the indicator style. Everything else looks fine for me.
设置指示器样式后,您应该更改颜色。其他一切对我来说都很好。

