我的应用正在使用WKWebView加载我的网站。我需要一个带有自定义深灰色背景视图的UiActivityIndicator,因为当它与我网站的内容混淆时,可能很难发现UIActivityIndicator。以前,我跟着https://coderwall.com/p/su1t1a/ios-customized-activity-indicator-with-swift以编程方式创建了一个指标。但是,本教程使用的是默认白色,而我的是大白色,并带有自定义橙色。我只需要一个带有backgroundView内部指示器的backgroundView。不知何故,它一直有效,直到我注意到指示器不是那个中心,它向左上方跑了一点。请看这张图片,
在我的iPhone 5上非常明显,等等。所以,我使用了另一种使用自动布局的方法。背景视图仍然无法正常工作。这是我的自动布局设置
这是我的编程部分。
loadingView.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
loadingView.center = self.view.center
loadingView.backgroundColor = UIColor(red:211/255,green:211/255,blue:211/255, alpha: 1)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 10
loadingIndicator.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
loadingIndicator.isOpaque = false
loadingIndicator.center = CGPoint(x: loadingView.frame.size.width / 2.0, y: loadingView.frame.size.height / 2.0)
loadingIndicator.backgroundColor = UIColor.gray
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
loadingIndicator.color = UIColor(red:232/255,green:126/255,blue:4/255,alpha:1.0)
loadingIndicator.hidesWhenStopped = true
loadingView.addSubview(loadingIndicator)
self.view.addSubview(loadingView)
我更喜欢使用自动布局。
答案 0 :(得分:0)
尝试更改此代码
let loadingView = UIView()
let loadingIndicator = UIActivityIndicatorView()
loadingView.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
loadingView.center = self.view.center
loadingView.backgroundColor = UIColor(red:211/255,green:211/255,blue:211/255, alpha: 1)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 10
loadingIndicator.frame = CGRect(x: 0, y: 0, width: 37, height: 37)
loadingIndicator.isOpaque = false
loadingIndicator.center = CGPoint(x: loadingView.bounds.size.width / 2, y: loadingView.bounds.size.height / 2)
loadingIndicator.backgroundColor = UIColor.gray
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
loadingIndicator.color = UIColor(red:232/255,green:126/255,blue:4/255,alpha:1.0)
loadingIndicator.hidesWhenStopped = true
loadingView.addSubview(loadingIndicator)
loadingIndicator.tag = 500
for subView in loadingView.subviews{
if subView.tag == 500{
print(subView.frame)
print("Bounds", subView.bounds)
}
}
loadingIndicator.startAnimating()
self.view.addSubview(loadingView)
根据指标视图的样式是whiteLarge,默认情况下它是37因此更改和检查。
对于自动布局:
答案 1 :(得分:0)