Ios 制作一个带有背景的UIActivityIndicator,在Swift中定位主视图的中心

Ios 制作一个带有背景的UIActivityIndicator,在Swift中定位主视图的中心,ios,swift,uiview,uiactivityindicatorview,Ios,Swift,Uiview,Uiactivityindicatorview,因此,我的目标是,在屏幕中央放置一个UIActivityIndicator,在微调器周围有一个背景,然后在半透明度下有一个完整的背景。此图显示了我正在尝试创建的内容: 我没有任何问题与微调器等,但得到所有层的位置正确。他们似乎都坐在中心以下。请参见下图,了解实际发生的情况: 代码如下: var spinningActivityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() let container: U

因此,我的目标是,在屏幕中央放置一个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()

有人能帮我解决这个问题吗?谢谢

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

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()
这种事情总是错的:

原因是,
center
是根据视图的superview坐标系计算的。由于这两个视图本身是彼此的子视图和超级视图,因此这两个
中心
值位于两个不同的坐标系中。因此,试图使它们彼此相等永远无法得到你想要的结果

相反,要理解框架和边界之间的区别。子视图的
(和
中心
)根据其超级视图的
边界
给出。因此,在其superview中居中视图的方法是将其
中心放置在点上

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

因此,决定您希望您的活动指示器位于哪个视图的中心,将您的活动指示器设置为该视图的子视图,并根据该公式设置其
中心
,您就可以找到正确的答案。

对于寻求更清晰答案的其他人(Swift 4


这正是我所需要的!谢谢-唯一需要更改的是行
loadingView.center=self.container.center
-它只需要
=container.center
。如果你更新了答案,我会为你做正确的标记:)问题是你不理解框架和边界之间的区别。这一行
loadingView.center=self.view.center
总是说得不对,因为这些值位于两个不同的坐标系中。
CGPointMake(
    CGRectGetMidX(theSuperview.bounds),
    CGRectGetMidY(theSuperview.bounds))
        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()