【问题标题】:Reposition the Activity Indicator inside UIAlertView在 UIAlertView 中重新定位活动指示器
【发布时间】:2017-03-16 17:17:29
【问题描述】:

我正在使用内部带有 Activity 指示器的 UIAlertView,但指示器的位置不正确。我确定我错过了一些非常小的东西,但如果有人能帮助我,那就太好了。下面是我的代码和对应的代码sn -p

var progressAlert: UIAlertView = UIAlertView(title: "Downloading", message: "Patients information...", delegate: nil, cancelButtonTitle: nil);


    var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
    loadingIndicator.center = CGPointMake(progressAlert.bounds.size.width/2, progressAlert.bounds.size.height - 150)
    loadingIndicator.hidesWhenStopped = true
    loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
    loadingIndicator.startAnimating()

    progressAlert.setValue(loadingIndicator, forKey: "accessoryView")
    progressAlert.show()

编辑:按照马特的回答,最终创建了自定义视图,它就像一个魅力。

【问题讨论】:

  • 它正处于我的中心。你能指定它的位置不正确吗?
  • 我正在尝试将活动指示器向上移动几个像素,因为您可以看到它粘在警报视图的底部。你知道怎么做吗?
  • 哦。现在我明白你在说什么了。

标签: ios swift uialertview uiactivityindicatorview


【解决方案1】:

这里有两个问题。一个是您正在对progressAlert 的最终大小进行假设 - 您正在根据progressAlert 框架现在 定位活动指示器,而不是基于它 >将是当它出现时。您可以通过使用约束而不是绝对的 center 值来解决这个问题。

但是,更大的问题是您所做的事情是非法的。不要将您自己的子视图添加到 UIAlertView。相反,用你自己的视图制作你自己的视图控制器并呈现它。它可以看起来像一个警报(即很小、居中、使界面的其余部分变暗),但现在它是你的视图,你可以在其中做任何你喜欢的事情。

【讨论】:

  • 好的,所以基本上你是说我需要创建一个自定义视图,我们不能修改 UIAlertView。那是我的计划 b,我想我会继续努力。谢谢马特
  • 这比您想象的要容易得多。随意从我的 github 示例中窃取代码!
  • 是的,我已经在我的代码中使用了类似的东西。我只是认为让 UIAlertView 做这一切会更容易,代码也更少。无论如何,我已经用活动指示器创建了我的自定义视图,它就像一个魅力。感谢马特的所有帮助:)
【解决方案2】:
import Foundation
import UIKit

class YourFunction{
//#MARK: - showAlertView With Activity indicator
class func showAlertViewWithIndicator(TitleMessage: String, timeInterval: Double,view: UIViewController){
    let alert = UIAlertView()
    alert.delegate = self
    alert.title = TitleMessage
    let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37)) as UIActivityIndicatorView
    loadingIndicator.center = view.view.center
    loadingIndicator.hidesWhenStopped = true
    loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
    loadingIndicator.startAnimating();

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

    alert.show()

    let delay = timeInterval * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue(), {
        alert.dismissWithClickedButtonIndex(-1, animated: true)

    loadingIndicator.stopAnimating()
    loadingIndicator.hidesWhenStopped = true
    view.dismissViewControllerAnimated(true, completion: nil)

    })

}
}

我为你编写了全局函数。这将显示带有标题和活动指示器的 UIAlertView。您可以根据 TimeInterval 变量设置时间。您可以在视图控制器中的任何位置调用它。只需输入 YourFunction.showAlertViewWithIndicator("Hello World",timeInterval:3.0,self)

它适用于 swift 2x 只是想帮忙:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多