“Please wait” dialog in iOS8

后端 未结 8 1708
离开以前
离开以前 2020-12-13 08:14

I used to have a \"Please wait\" dialog in my app for long time. It was quite simple thing using UIActivityIndicatorView and adding it to

相关标签:
8条回答
  • 2020-12-13 08:32

    Swift 3.1 version of @darksinge code

    import UIKit
    
    class ActivityViewController: UIViewController {
    
        private let activityView = ActivityView()
    
        init(message: String) {
            super.init(nibName: nil, bundle: nil)
            modalTransitionStyle = .crossDissolve
            modalPresentationStyle = .overFullScreen
            activityView.messageLabel.text = message
            view = activityView
        }
    
        required init(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
    private class ActivityView: UIView {
    
        let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
        let boundingBoxView = UIView(frame: CGRect.zero)
        let messageLabel = UILabel(frame: CGRect.zero)
    
        init() {
            super.init(frame: CGRect.zero)
    
            backgroundColor = UIColor(white: 0.0, alpha: 0.5)
    
            boundingBoxView.backgroundColor = UIColor(white: 0.0, alpha: 0.5)
            boundingBoxView.layer.cornerRadius = 12.0
    
            activityIndicatorView.startAnimating()
    
            messageLabel.font = UIFont.boldSystemFont(ofSize: UIFont.labelFontSize)
            messageLabel.textColor = UIColor.white
            messageLabel.textAlignment = .center
            messageLabel.shadowColor = UIColor.black
            messageLabel.shadowOffset = CGSize(width: 0.0, height: 1.0)
            messageLabel.numberOfLines = 0
    
            addSubview(boundingBoxView)
            addSubview(activityIndicatorView)
            addSubview(messageLabel)
        }
    
        required init(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            boundingBoxView.frame.size.width = 160.0
            boundingBoxView.frame.size.height = 160.0
            boundingBoxView.frame.origin.x = ceil((bounds.width / 2.0) - (boundingBoxView.frame.width / 2.0))
            boundingBoxView.frame.origin.y = ceil((bounds.height / 2.0) - (boundingBoxView.frame.height / 2.0))
    
            activityIndicatorView.frame.origin.x = ceil((bounds.width / 2.0) - (activityIndicatorView.frame.width / 2.0))
            activityIndicatorView.frame.origin.y = ceil((bounds.height / 2.0) - (activityIndicatorView.frame.height / 2.0))
    
            let messageLabelSize = messageLabel.sizeThatFits(CGSize(width:160.0 - 20.0 * 2.0, height: CGFloat.greatestFiniteMagnitude))
            messageLabel.frame.size.width = messageLabelSize.width
            messageLabel.frame.size.height = messageLabelSize.height
            messageLabel.frame.origin.x = ceil((bounds.width / 2.0) - (messageLabel.frame.width / 2.0))
            messageLabel.frame.origin.y = ceil(activityIndicatorView.frame.origin.y + activityIndicatorView.frame.size.height + ((boundingBoxView.frame.height - activityIndicatorView.frame.height) / 4.0) - (messageLabel.frame.height / 2.0))
        }
    }
    
    0 讨论(0)
  • 2020-12-13 08:43

    Swift 3.0/4.1

    To show the progress dialog:

    let alertController = UIAlertController(title: nil, message: "Please wait\n\n", preferredStyle: .alert)
    
    let spinnerIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
    
    spinnerIndicator.center = CGPoint(x: 135.0, y: 65.5)
    spinnerIndicator.color = UIColor.black
    spinnerIndicator.startAnimating()
    
    alertController.view.addSubview(spinnerIndicator)
    self.present(alertController, animated: false, completion: nil)
    

    To dismiss the progress dialog:

    alertController.dismiss(animated: true, completion: nil);
    
    0 讨论(0)
提交回复
热议问题