如何将消息文本与iOS XCode上的活动指示器视图放在一起?

时间:2018-03-15 18:55:08

标签: ios xcode uiactivityindicatorview

我有一个活动指示器来通知用户等待登录webservice,但只是显示一个圆圈,我想要一起设置一个文本,如"登录,等待......"

@IBOutlet weak var waitView: UIActivityIndicatorView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.waitView.startAnimating()
    self.waitView.hidesWhenStopped = true

    OdooAuth.init( successBlock: {
        result in
        self.waitView.stopAnimating()
        print("Auth Success: \(result)")
    }, failureBlock: {
        result in
        self.waitView.stopAnimating()
        print("Auth Error: \(result)")
    })
}

2 个答案:

答案 0 :(得分:0)

你可以这样使用:

@IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!

let viewForActivityIndicator = UIView()
let view: UIView
let loadingTextLabel = UILabel()

override func viewDidLoad() {
    super.viewDidLoad()
    self.showActivityIndicator()

    OdooAuth.init( successBlock: {
        result in
        self.stopActivityIndicator()
        print("Auth Success: \(result)")
    }, failureBlock: {
        result in
        self.stopActivityIndicator()
        print("Auth Error: \(result)")
    })
}

func showActivityIndicator() {
    viewForActivityIndicator.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: self.view.frame.size.height)
    viewForActivityIndicator.backgroundColor = UIColor.white
    view.addSubview(viewForActivityIndicator)

    activityIndicatorView.center = CGPoint(x: self.view.frame.size.width / 2.0, y: self.view.frame.size.height / 2.0)

    loadingTextLabel.textColor = UIColor.black
    loadingTextLabel.text = "LOADING"
    loadingTextLabel.font = UIFont(name: "Avenir Light", size: 10)
    loadingTextLabel.sizeToFit()
    loadingTextLabel.center = CGPoint(x: activityIndicatorView.center.x, y: activityIndicatorView.center.y + 30)
    viewForActivityIndicator.addSubview(loadingTextLabel)

    activityIndicatorView.hidesWhenStopped = true
    activityIndicatorView.activityIndicatorViewStyle = .gray
    viewForActivityIndicator.addSubview(activityIndicatorView)
    activityIndicatorView.startAnimating()
}

func stopActivityIndicator() {
    viewForActivityIndicator.removeFromSuperview()
    activityIndicatorView.stopAnimating()
    activityIndicatorView.removeFromSuperview()
}

答案 1 :(得分:0)

对于Swift 5

WKWebview中带有标签的指示器

    var strLabel = UILabel()
   let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
    let loadingTextLabel = UILabel()
   
    @IBOutlet var indicator: UIActivityIndicatorView!
    @IBOutlet var webView: WKWebView!
    
    var refController:UIRefreshControl = UIRefreshControl()
    
    
    override func viewDidLoad() {
        webView = WKWebView(frame: CGRect.zero)
        webView.navigationDelegate = self
        webView.uiDelegate = self as? WKUIDelegate
        
        let preferences = WKPreferences()
        preferences.javaScriptEnabled = true
        let configuration = WKWebViewConfiguration()
        configuration.preferences = preferences
       
        
        webView.allowsBackForwardNavigationGestures = true
        
        webView.load(URLRequest(url: URL(string: "https://www.google.com")!))
        setBackground()
        
    }
    

    
    func setBackground() {
        view.addSubview(webView)
        webView.translatesAutoresizingMaskIntoConstraints = false
        webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        webView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        webView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    }
    
    func showActivityIndicator(show: Bool) {
        if show {
            
            
            strLabel = UILabel(frame: CGRect(x: 55, y: 0, width: 400, height: 66))
            strLabel.text = "Please Wait. Checking Internet Connection..."
            strLabel.font = UIFont(name: "Avenir Light", size: 12)
            
           
            strLabel.textColor = UIColor(white: 0.9, alpha: 0.7)
           
            effectView.frame = CGRect(x: view.frame.midX - strLabel.frame.width/2, y: view.frame.midY - strLabel.frame.height/2 , width: 300, height: 66)
            effectView.layer.cornerRadius = 15
            effectView.layer.masksToBounds = true
            indicator = UIActivityIndicatorView(style: .white)
            indicator.frame = CGRect(x: 0, y: 0, width: 66, height: 66)
            indicator.startAnimating()
            effectView.contentView.addSubview(indicator)
            effectView.contentView.addSubview(strLabel)
            indicator.transform = CGAffineTransform(scaleX: 1.4, y: 1.4);
            effectView.center = webView.center
            view.addSubview(effectView)
            
            
        } else {
            strLabel.removeFromSuperview()
             effectView.removeFromSuperview()
            indicator.removeFromSuperview()
            indicator.stopAnimating()
        }
    }

enter image description here