ios 如何快速使用 MBProgressHUD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26881625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use MBProgressHUD with swift
提问by vimal prakash
here is my code , but it showing the progress . is there any error in this code? please give some idea to fix this, or give some link related to this.
这是我的代码,但它显示了进度。这段代码有什么错误吗?请给出一些想法来解决这个问题,或者给出一些与此相关的链接。
class Approval: UIViewController {
var hud: MBProgressHUD = MBProgressHUD()
override func viewDidLoad() {
super.viewDidLoad()
fetchData()
}
func fetchData(){
hud.show(true)
// doing some http request
dispatch_async(dispatch_get_main_queue()) {
hud.hide(true)
}
}
}
回答by EricDXS
Updated Answer:
更新答案:
let loadingNotification = MBProgressHUD.showAdded(to: view, animated: true)
loadingNotification.mode = MBProgressHUDMode.indeterminate
loadingNotification.label.text = "Loading"
To dismiss the ProgressHUD:
要关闭 ProgressHUD:
MBProgressHUD.hideAllHUDs(for: view, animated: true)
回答by The Crazy Chimp
You can also try this approach which will keep the other activity running in the background allowing the UI to remain responsive, providing users with a better experience. This is the intended/recommended approach for using the MBProgressHUD.
您也可以尝试这种方法,它可以让其他 Activity 在后台运行,从而让 UI 保持响应,为用户提供更好的体验。这是使用 MBProgressHUD 的预期/推荐方法。
let progressHUD = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
progressHUD.labelText = "Loading..."
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)) {
// ...Run some task in the background here...
dispatch_async(dispatch_get_main_queue()) {
progressHUD.hide(true)
// ...Run something once we're done with the background task...
}
}
回答by Chris Van Buskirk
Swift 3 extensions
斯威夫特 3 扩展
import Foundation
import MBProgressHUD
import QuartzCore
extension UITableViewController {
func showHudForTable(_ message: String) {
let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
hud.label.text = message
hud.isUserInteractionEnabled = false
hud.layer.zPosition = 2
self.tableView.layer.zPosition = 1
}
}
extension UIViewController {
func showHud(_ message: String) {
let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
hud.label.text = message
hud.isUserInteractionEnabled = false
}
func hideHUD() {
MBProgressHUD.hide(for: self.view, animated: true)
}
}
// Use extensions
回答by Harjot Singh
Create Extension to Easy to use and throughout application
创建易于使用和整个应用程序的扩展
extension UIViewController {
func showHUD(progressLabel:String){
DispatchQueue.main.async{
let progressHUD = MBProgressHUD.showAdded(to: self.view, animated: true)
progressHUD.label.text = progressLabel
}
}
func dismissHUD(isAnimated:Bool) {
DispatchQueue.main.async{
MBProgressHUD.hide(for: self.view, animated: isAnimated)
}
}
}
USAGE:
用法:
1. SHOW- self.showHUD(progressLabel: "Loading...")
1. 显示- self.showHUD(progressLabel: "Loading...")
2. HIDE- self.dismissHUD(isAnimated: true)
2.隐藏- self.dismissHUD(isAnimated: true)
回答by user3182143
Go through the below code
通过下面的代码
class ViewController: UIViewController, MBProgressHUDDelegate {
var hud : MBProgressHUD = MBProgressHUD()
func fetchData() {
hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
hud.mode = MBProgressHUDModeIndeterminate
hud.labelText = "Loading"
}
}
If you want to dismiss the HUD
如果您想关闭 HUD
MBProgressHUD.hideHUDForView(self.view, animated: true)
回答by Bharathi Devarasu
Use the below code to render MBProgressHUD and after some action completed hide it as described.
使用以下代码呈现 MBProgressHUD,并在完成某些操作后按描述隐藏它。
let spinnerActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true);
spinnerActivity.label.text = "Loading";
spinnerActivity.detailsLabel.text = "Please Wait!!";
spinnerActivity.userInteractionEnabled = false;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0))
{
// Add some background task like image download, wesite loading.
dispatch_async(dispatch_get_main_queue())
{
spinnerActivity.hideAnimated(true);
}
}
For more information follow this tutorial: http://sourcefreeze.com/mbprogresshud-example-swift/
有关更多信息,请参阅本教程:http: //sourcefreeze.com/mbprogresshud-example-swift/
回答by Janmenjaya
Added to @EricDXS's answer,
添加到@EricDXS 的回答中,
The Swift 3 version is here
Swift 3 版本在这里
To show:
显示:
let loadingNotification = MBProgressHUD.showAdded(to: self.view, animated: true)
loadingNotification.mode = MBProgressHUDMode.indeterminate
loadingNotification.label.text = "Loading"
To dismiss:
解雇:
loadingNotification.hide(animated: true)
回答by javimuu
Try it with Swift 3:
用 Swift 3 试试:
func showLoadingHUD(to_view: UIView) {
let hud = MBProgressHUD.showAdded(to: to_view, animated: true)
hud.label.text = "Loading..."
}
func hideLoadingHUD(for_view: UIView) {
MBProgressHUD.hideAllHUDs(for: for_view, animated: true)
}
If Swift compiler warning: hideAllHUDs is deprecated. We should store references when using more than one HUD per view
如果 Swift 编译器警告: hideAllHUDs is deprecated. We should store references when using more than one HUD per view
Use:
用:
func hideLoadingHUD(for_view: UIView) {
MBProgressHUD.hide(for: for_view, animated: true)
}
回答by Krutagn Patel
step 1 : Download "MBProgressHUD.h" and "MBProgressHUD.m" file and Add both file into your project. it will ask you to Bridging for Objective C files. if you already done bridging then it won't ask.
第 1 步:下载“MBProgressHUD.h”和“MBProgressHUD.m”文件并将这两个文件添加到您的项目中。它会要求您桥接 Objective C 文件。如果你已经完成了桥接,那么它就不会问了。
step 2 : In Bridging File import MBProgressHUD "import MBProgressHUD.h"
第 2 步:在桥接文件中导入 MBProgressHUD "import MBProgressHUD.h"
step 3 : use below code to show progress hud or hide hud.
第 3 步:使用以下代码显示进度 hud 或隐藏 hud。
for Show
用于展示
DispatchQueue.main.async {
MBProgressHUD.showAdded(to: self.view, animated: true)
}
for hide
为了隐藏
DispatchQueue.main.async {
MBProgressHUD.hide(for: self.view, animated: true)
}
回答by vimal prakash
I solved it like this:
我是这样解决的:
import UIKit
class Loader: NSObject {
class func show(message:String = "Processing...", delegate: UIViewController) {
var load : MBProgressHUD = MBProgressHUD()
load = MBProgressHUD.showHUDAddedTo(delegate.view, animated: true)
load.mode = MBProgressHUDMode.Indeterminate
if message.length > 0 {
load.labelText = message;
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
class func hide(delegate:UIViewController) {
MBProgressHUD.hideHUDForView(delegate.view, animated: true)
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
}