视图

时间:2016-06-13 07:24:34

标签: ios swift uiview uiactivityindicatorview

在我的应用中,我使用了自定义的UIActivityIndi​​cator视图(this),效果很好。 我有一个UITableView,我希望在tableview上加载数据的指标。为了解决这个问题,我创建了一个带有背景的空视图,当我需要启动指示器动画时,我只需隐藏tableview并显示空视图+指示器。

结果如下:

Image 1

我看到某个地方我可以使用这样的东西:

Image 2

我该怎么办?

提前致谢。

2 个答案:

答案 0 :(得分:1)

您可以尝试在表视图上添加半透明黑色视图,并将自定义活动指示器代码作为子视图放在黑色视图中。

let aBlackView = UIView(frame: CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height))
aBlackView.backgroundColor = UIColor.blackColor()
aBlackView.alpha = 0.75 // Change the alpha depending on how much transparency you require

let aMainWindow = UIApplication.sharedApplication().delegate!.window
aMainWindow!!.addSubview(aBlackView)

/* Enter you code for NVActivityIndicatorViewable here */

aBlackView.addSubview(/* NVActivityIndicatorViewable */)

完成请求后,您可以通过调用以下代码删除黑色视图:

aBlackView.removeFromSuperview()

答案 1 :(得分:1)

我创建了一个显示屏幕的活动。我在项目的每个地方都使用过。

// ProgressBarNotification.swift

 import Foundation
 import UIKit

class ProgressBarNotification {

internal static var strLabel = UILabel()
internal static var messageFrame = UIView()
internal static var activityIndicator = UIActivityIndicatorView()    

internal static func progressBarDisplayer(msg:String,indicator:Bool ){


    let view1 = UIApplication.sharedApplication().delegate?.window!!.rootViewController?.view

    view1?.userInteractionEnabled = false

    strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
    strLabel.text = msg
    strLabel.textColor = UIColor.whiteColor()
    messageFrame = UIView(frame: CGRect(x: view1!.frame.midX - 90, y: view1!.frame.midY - 25 , width: 180, height: 50))
    messageFrame.layer.cornerRadius = 15
    messageFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
    if indicator {
        activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
        activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        activityIndicator.startAnimating()
        messageFrame.addSubview(activityIndicator)
    }
    messageFrame.addSubview(strLabel)
    view1!.addSubview(messageFrame)


}

internal static func removeProgressBar() {

    let view1 = UIApplication.sharedApplication().delegate?.window!!.rootViewController?.view

    _ = view1?.subviews.filter({ (view) -> Bool in
        if view == ProgressBarNotification.messageFrame {
            view.removeFromSuperview()
        }
        return false
    })

    ProgressBarNotification.messageFrame.removeFromSuperview()

    view1?.userInteractionEnabled = true

}

  deinit{

  }
}

用法

    //To start Loader

    ProgressBarNotification.progressBarDisplayer("Loading", indicator: true)

   //Stop 

    ProgressBarNotification.removeProgressBar()