调整 UIActivityIndi​​catorView 在 AlertView 中的位置 - Swift

Adjust UIActivityIndicatorView position in AlertView - Swift

我一直在尝试调整 IndicatorView 在 UIAlertView 中的位置,但始终没有用。我尝试设置 loadingIndicator loadingIndicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50) 但它根本不适用,看看这张照片:

alert = UIAlertView(title: "Loading...", message: nil , delegate: nil, cancelButtonTitle: nil)
        loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37))
        loadingIndicator.center = self.view.center
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        loadingIndicator.startAnimating();

        alert.setValue(loadingIndicator, forKey: "accessoryView")
        loadingIndicator.startAnimating()
        alert.show();

我尝试了很多次向 UIAlertView 添加自定义视图,但发生了很多奇怪的事情。你可以选择使用这个库,它不像 UIAlertView 那样漂亮,它可能会解决你的问题。

Custom iOS AlertView

尝试以下代码:

        var alert : UIAlertView = UIAlertView(title: "Loading...", message: nil , delegate: nil, cancelButtonTitle: nil)
        var viewBack:UIView = UIView(frame: CGRectMake(83,0,100,100))

        var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37))
        loadingIndicator.center = viewBack.center
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        loadingIndicator.startAnimating();
        viewBack.addSubview(loadingIndicator)
        viewBack.center = self.view.center
        alert.setValue(viewBack, forKey: "accessoryView")
        loadingIndicator.startAnimating()
        alert.show();

您可以根据需要在消息中使用尽可能多的 \n 来创建 space。

在你的ViewController.swift中:

lazy var alertController: UIAlertController = {
    let alert = UIAlertController(title: "Loading", message: "\n\n", preferredStyle: .alert)
    alert.view.tintColor = .black
    let loading = UIActivityIndicatorView(frame: CGRect(x: 110, y: 35, width: 50, height: 50))
    loading.hidesWhenStopped = true
    loading.style = .gray
    loading.startAnimating();
    alert.view.addSubview(loading)
    return alert
}()

和用法:

// to show loading wheel
present(alertController, animated: true)

// to hide loading wheel
dismiss(animated: true)

视觉输出:

(灵感来自@AshishKakkad 的回答)