【问题标题】:Adjust UIActivityIndicatorView position in AlertView - Swift调整 AlertView 中的 UIActivityIndi​​catorView 位置 - Swift
【发布时间】:2015-05-02 13:05:30
【问题描述】:

我一直在尝试在 UIAlertView 中调整 IndicatorView 的位置,但它始终不起作用。我尝试设置 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();

【问题讨论】:

标签: ios swift uialertview uiactivityindicatorview


【解决方案1】:

试试下面的代码:

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

【讨论】:

  • 但它变得如此巨大,只是将指示器稍微调整到顶部而不会创建巨大的视图以始终位于警报的中心?
  • 只要改变viewBack的大小,你会得到更小的视图:) @AaoIi
  • var viewBack:UIView = UIView(frame: CGRectMake(83,0,100,60))
  • 好的,那就去吧@AaoIi
  • 怎么放底部?
【解决方案2】:

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

Custom iOS AlertView

【讨论】:

    【解决方案3】:

    您可以在消息中根据需要使用任意数量的\n 来创建空间。

    在你的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 回答的启发)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多