【问题标题】:Make a UIActivityIndicator with background, position centre of the main View in Swift制作一个带有背景的 UIActivityIndi​​cator,在 Swift 中主视图的位置中心
【发布时间】:2016-02-09 19:40:03
【问题描述】:

所以我的目标是定位在屏幕中心的 UIActivityIndi​​cator,在微调器周围有一个背景,然后在半透明度下有一个完整的背景。这张图片显示了我正在尝试创建的内容:

我对微调器等没有任何问题,但是可以正确定位所有图层。他们似乎都坐在中心下方。请看下图,了解实际情况:

代码如下:

var spinningActivityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()

    let container: UIView = UIView()
    container.frame = self.view.frame
    container.center = self.view.center
    container.backgroundColor = UIColor(hue: 0/360, saturation: 0/100, brightness: 0/100, alpha: 0.4)

    let loadingView: UIView = UIView()
    loadingView.frame = CGRectMake(0, 0, 80, 80)
    loadingView.center = self.view.center
    loadingView.backgroundColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)
    loadingView.clipsToBounds = true
    loadingView.layer.cornerRadius = 40

    spinningActivityIndicator.frame = CGRectMake(0, 0, 40, 40)
    spinningActivityIndicator.hidesWhenStopped = true
    spinningActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    spinningActivityIndicator.center = CGPointMake(loadingView.frame.size.width / 2, loadingView.frame.size.height / 2)
    loadingView.addSubview(spinningActivityIndicator)
    container.addSubview(loadingView)
    view.addSubview(container)
    spinningActivityIndicator.startAnimating()
    UIApplication.sharedApplication().beginIgnoringInteractionEvents()

谁能帮我解决这个问题?谢谢!

【问题讨论】:

  • 问题是你不明白frame和bounds的区别。这条线loadingView.center = self.view.center 总是说错了,因为这些值位于两个不同的坐标系中。

标签: ios swift uiview uiactivityindicatorview


【解决方案1】:

您可以直接将其添加到应用程序窗口,而不是将其添加到您的视图中。如果您使用窗口,您的容器可以覆盖整个屏幕。你可以试试这个吗?

var spinningActivityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
let window = UIApplication.sharedApplication().keyWindow
let container: UIView = UIView()
container.frame = UIScreen.mainScreen().bounds
container.backgroundColor = UIColor(hue: 0/360, saturation: 0/100, brightness: 0/100, alpha: 0.4)

let loadingView: UIView = UIView()
loadingView.frame = CGRectMake(0, 0, 80, 80)
loadingView.center = container.center
loadingView.backgroundColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 40

spinningActivityIndicator.frame = CGRectMake(0, 0, 40, 40)
spinningActivityIndicator.hidesWhenStopped = true
spinningActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
spinningActivityIndicator.center = CGPointMake(loadingView.frame.size.width / 2, loadingView.frame.size.height / 2)
loadingView.addSubview(spinningActivityIndicator)
container.addSubview(loadingView)
window.addSubview(container)
spinningActivityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()

对于停止指示器和删除视图使用

spinningActivityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
container.removeFromSuperview()

【讨论】:

  • 这完全符合我的需要!谢谢 - 唯一需要的更改是在线 loadingView.center = self.container.center - 它只需要是 = container.center 。如果您更新答案,我会为您标记正确的:)
【解决方案2】:

这种事情总是错的:

loadingView.center = self.view.center

原因是center 是根据视图的父视图的坐标系来计算的。由于这两个视图本身是彼此的子视图和父视图,因此这两个center 值位于两个不同的坐标系中。因此,试图将它们设置为彼此相等永远无法得到您想要的结果。

相反,请了解框架和边界之间的区别。子视图的frame(和center)以其父视图的bounds 给出。因此,在其父视图中居中视图的方法是将其center 放在该点

CGPointMake(
    CGRectGetMidX(theSuperview.bounds),
    CGRectGetMidY(theSuperview.bounds))

因此,决定您希望活动指示器位于哪个视图的中心,将活动指示器设置为该视图的子视图,并根据该公式设置其 center,然后您就会发现。

【讨论】:

    【解决方案3】:

    对于寻求更简洁答案的其他人 (Swift 4)

            self.activityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    
            self.activityIndicatorView.hidesWhenStopped = true
            self.activityIndicatorView.style = .whiteLarge
            self.activityIndicatorView.backgroundColor = UIColor.darkGray.withAlphaComponent(0.75)
    
            UIApplication.shared.keyWindow?.addSubview(self.activityIndicatorView)
    
            self.activityIndicatorView.startAnimating()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多