我正在尝试以编程方式在警报控制器中居中活动指示器,但没有看到预期的结果。
尝试:
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)
}
答案 0 :(得分:0)
您面临的问题在于此行:
let indicator = NVActivityIndicatorView(frame: CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50))
此时,您的pending
警报控制器尚未出现 ,并且其框架未更新。
在显示警报控制器之后,您将必须更新指示器的frame
,例如,您可以执行以下操作。
替换此行:
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()
})