所以我的目标是,UIActivityIndicator位于屏幕中央,背景是旋转器,然后是半透明的全背景。此图显示了我要创建的内容:
我对旋转器等没有任何问题,但是让所有层都正确定位。他们似乎都坐在中心下面。请参阅下面的实际情况:
代码如下:
var spinningActivityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
let container: UIView = UIView()
container.frame = self.view.frame
container.center = self.view.center
container.backgroundColor = UIColor(hue: 0/360, saturation: 0/100, brightness: 0/100, alpha: 0.4)
let loadingView: UIView = UIView()
loadingView.frame = CGRectMake(0, 0, 80, 80)
loadingView.center = self.view.center
loadingView.backgroundColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 40
spinningActivityIndicator.frame = CGRectMake(0, 0, 40, 40)
spinningActivityIndicator.hidesWhenStopped = true
spinningActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
spinningActivityIndicator.center = CGPointMake(loadingView.frame.size.width / 2, loadingView.frame.size.height / 2)
loadingView.addSubview(spinningActivityIndicator)
container.addSubview(loadingView)
view.addSubview(container)
spinningActivityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
任何人都可以帮我解决这个问题吗?谢谢!
答案 0 :(得分:2)
您可以将其直接添加到应用程序窗口,而不是将其添加到视图中。如果您使用窗口,您的容器可以覆盖整个屏幕。你能尝试一下吗?
var spinningActivityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
let window = UIApplication.sharedApplication().keyWindow
let container: UIView = UIView()
container.frame = UIScreen.mainScreen().bounds
container.backgroundColor = UIColor(hue: 0/360, saturation: 0/100, brightness: 0/100, alpha: 0.4)
let loadingView: UIView = UIView()
loadingView.frame = CGRectMake(0, 0, 80, 80)
loadingView.center = container.center
loadingView.backgroundColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 40
spinningActivityIndicator.frame = CGRectMake(0, 0, 40, 40)
spinningActivityIndicator.hidesWhenStopped = true
spinningActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
spinningActivityIndicator.center = CGPointMake(loadingView.frame.size.width / 2, loadingView.frame.size.height / 2)
loadingView.addSubview(spinningActivityIndicator)
container.addSubview(loadingView)
window.addSubview(container)
spinningActivityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
停止指示并删除视图使用
spinningActivityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
container.removeFromSuperview()
答案 1 :(得分:0)
这种事情总是错误:
loadingView.center = self.view.center
原因是center
根据视图的superview坐标系来计算。由于这两个视图本身是彼此的子视图和超视图,因此两个center
值位于两个不同的坐标系中。因此,试图将它们设置为彼此相等无法获得您想要的结果。
相反,要了解框架和边界之间的区别。子视图的frame
(和center
)是根据其超级视图的bounds
给出的。因此,在超级视图中居中视图的方法是将其center
置于
CGPointMake(
CGRectGetMidX(theSuperview.bounds),
CGRectGetMidY(theSuperview.bounds))
因此,请确定您希望活动指标位于哪个视图的中心,使您的活动指示符显示该视图的子视图,并根据该公式设置其center
,并且您&#39 ;请点亮。
答案 2 :(得分:0)
对于其他寻求更简洁答案的人(迅速4 )
self.activityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
self.activityIndicatorView.hidesWhenStopped = true
self.activityIndicatorView.style = .whiteLarge
self.activityIndicatorView.backgroundColor = UIColor.darkGray.withAlphaComponent(0.75)
UIApplication.shared.keyWindow?.addSubview(self.activityIndicatorView)
self.activityIndicatorView.startAnimating()