Ios 以编程方式在alertcontroller中居中显示指示器视图

Ios 以编程方式在alertcontroller中居中显示指示器视图,ios,swift,uiview,constraints,autoresizingmask,Ios,Swift,Uiview,Constraints,Autoresizingmask,我试图以编程方式将活动指示器集中在警报控制器中,但没有看到预期结果 尝试: import NVActivityIndicatorView public func displayActivityAlertWithCompletion(ViewController: UIViewController, pending: UIAlertController, completionHandler: @escaping ()->()) { //create an activity ind

我试图以编程方式将活动指示器集中在警报控制器中,但没有看到预期结果

尝试:

import NVActivityIndicatorView

public func displayActivityAlertWithCompletion(ViewController: UIViewController, pending: UIAlertController, completionHandler: @escaping ()->())
{

    //create an activity indicator
    let indicator = NVActivityIndicatorView(frame: CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50))


    //indicator.clipsToBounds = true
    indicator.type = .ballScaleMultiple
    indicator.autoresizingMask = \[.flexibleWidth, .flexibleHeight\]
    indicator.color = UIColor(rgba: Palette.loadingColour)



    //add the activity indicator as a subview of the alert controller's view
    pending.view.addSubview(indicator)
    indicator.isUserInteractionEnabled = false
    // required otherwise if there buttons in the UIAlertController you will not be able to press them
    indicator.startAnimating()



    ViewController.present(pending, animated: true, completion: completionHandler)
}

您面临的问题在于这一行:

let indicator = NVActivityIndicatorView(frame: CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50))
ViewController.present(pending, animated: true, completion: completionHandler)
此时,您的
挂起
警报控制器尚未显示,且其框架未更新

出现警报控制器后,您必须更新指示器的
,例如,您可以执行以下操作

替换此行:

let indicator = NVActivityIndicatorView(frame: CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50))
ViewController.present(pending, animated: true, completion: completionHandler)
为此:

ViewController.present(pending, animated: true, completion: {
    // update frame here:
    indicator.frame = CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50)
    // and manually call completionHandler:
    completionHandler()
})

你看到了什么?@MilanNosáľ很抱歉,我添加了一张图片,但是代码的语法混乱了,重新添加了它。你从这个
挂起的.view.frame.width中得到了什么价值?从何处调用方法
displayActivityAlertWithCompletion