制作一个带有背景的 UIActivityIndi​​cator,位于 Swift 中主视图的中心

Make a UIActivityIndicator with background, position centre of the main View in Swift

所以我的目标是将 UIActivityIndi​​cator 定位在屏幕中央,微调器周围有背景,然后是半透明的完整背景。此图像显示了我正在尝试创建的内容:

我对微调器等没有任何问题,但所有图层都正确定位。他们似乎都坐在中心下方。请查看下图了解实际情况:

代码如下:

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

谁能帮我解决这个问题?谢谢!

您可以直接将其添加到应用程序 window,而不是将其添加到您的视图中。如果你使用 window,你的容器可以覆盖整个屏幕。你能试试这个吗?

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

这种事情总是错误的:

loadingView.center = self.view.center

原因是center是根据视图的父视图的坐标系计算的。由于这两个视图本身是彼此的子视图和父视图,因此这两个 center 值位于 两个不同的坐标系 中。因此,试图将它们设置为彼此相等永远无法获得您想要的结果。

相反,了解框架和边界之间的区别。子视图的 frame(和 center)根据其父视图的 bounds 给出。因此,在其父视图中将视图居中的方法是将其 center 放在

CGPointMake(
    CGRectGetMidX(theSuperview.bounds),
    CGRectGetMidY(theSuperview.bounds))

因此,决定您希望 activity 指示器位于哪个视图的中心,使您的 activity 指示器成为视图的子视图,并根据该视图设置其 center公式,你会发现。

对于其他寻求更清晰答案的人 (Swift 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()