//
// MBProgressHUD+extension.swift
// YouWen
//
// Created by liuyaqiang on 2020/11/20.
//
import UIKit
import MBProgressHUD
let HUD_Duration_Infinite = -1
let HUD_Duration_Normal = 1.5
let HUD_Duration_Short = 0.5
extension MBProgressHUD {
@discardableResult
class func showAdded(view: UIView, duration showTime: Double, animated: Bool) -> (MBProgressHUD) {
var animated = animated
let existHUD:MBProgressHUD = MBProgressHUD(view: view)
if existHUD != nil{
// 如果有HUD先隐藏掉,保证始终只有一个HUD
MBProgressHUD.hide(for: view, animated: false)
// 如果有HUD,则不做animation
animated = false
}
let showView = self.showAdded(to: view, animated: animated)
if Int(showTime) != HUD_Duration_Infinite {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(showTime * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {
MBProgressHUD.hide(for: view, animated: false)
})
}
return showView
}
@discardableResult
class func showAdded(view: UIView, duration showTime: TimeInterval, withText text: String?, animated: Bool) -> (MBProgressHUD) {
let showView = self.showAdded(view: view, duration: showTime, animated: animated)
MBProgressHUD(view: view).isUserInteractionEnabled = false
MBProgressHUD(view: view).mode = .text
MBProgressHUD(view: view).label.text = text
return showView
}
@discardableResult
class func showAdded(view: UIView, icon image: UIImage?, duration showTime: TimeInterval, withText text: String?, animated: Bool) -> (MBProgressHUD) {
let showView = self.showAdded(view: view, duration: showTime, animated: animated)
MBProgressHUD(view: view).isUserInteractionEnabled = false
MBProgressHUD(view: view).mode = .customView
return showView
}
}
private let minHUDWidth: CGFloat = 0.0
extension UIViewController {
@discardableResult
func showWaitHUD() -> MBProgressHUD {
return showWaitHUD(title: "加载中…")
}
@discardableResult
func showWaitHUD(title: String?) -> MBProgressHUD {
let HUD = showHUD(title: title, duration: TimeInterval(HUD_Duration_Infinite))
HUD.isUserInteractionEnabled = true
return HUD
}
@discardableResult
func showHUDWithError(error: YWError) -> MBProgressHUD {
return showHUD(title: error.msg as String, duration: HUD_Duration_Normal)
}
@discardableResult
func showHUD(title: String?) -> MBProgressHUD {
return showHUD(title: title, duration: HUD_Duration_Normal)
}
@discardableResult
func showHUD(title: String?, detail: String?) -> MBProgressHUD {
return showHUD(title: title, detail: detail, duration: HUD_Duration_Normal)
}
@discardableResult
func showHUD(title: String?, duration: TimeInterval) -> MBProgressHUD {
return showHUD(title: "", detail: title, duration: duration)
}
@discardableResult
func showHUD(title: String?, detail: String?, duration: TimeInterval) -> MBProgressHUD {
var HUD:MBProgressHUD = MBProgressHUD()
if hudContainerView() != nil {
HUD = MBProgressHUD.showAdded(view: hudContainerView()!, duration: duration, withText: title, animated: true)
HUD.detailsLabel.text = detail
setHUDBelowNavigationBar()
HUD.minSize = CGSize(width: minHUDWidth, height: 0.0)
}
return HUD
}
@discardableResult
func showHUD(withTitle title: String?, detail: String?, topIcon iconImage: UIImage?, duration: TimeInterval) -> MBProgressHUD {
let HUD = showHUD(title: title, detail: detail, duration: duration)
HUD.mode = .customView
HUD.customView = UIImageView(image: iconImage)
return HUD
}
@discardableResult
func showHUD(withTitle title: String?, error: Error?) -> MBProgressHUD {
let errorDetail = error?.localizedDescription
return showHUD(title: title, detail: errorDetail)
}
func hideHUD() {
if hudContainerView() != nil {
MBProgressHUD.hide(for: hudContainerView()!, animated: true)
}
}
@discardableResult
func hudContainerView() -> UIView? {
if isViewLoaded {
if parent != nil && parent != navigationController {
return parent?.hudContainerView()
} else if navigationController?.parent != nil && navigationController?.parent != tabBarController {
return navigationController?.parent?.hudContainerView()
} else {
return view
}
} else {
return nil
}
}
func setHUDBelowNavigationBar() {
if navigationController?.view == hudContainerView() {
if let navigationBar = navigationController?.navigationBar {
if let hud = MBProgressHUD.forView((navigationController?.view)!) {
navigationController?.view.insertSubview(hud, belowSubview: navigationBar)
}
}
}
}
}
extension UIView {
@discardableResult
func showWaitHUD() -> MBProgressHUD {
return showWaitHUD(title: "加载中")
}
@discardableResult
func showHUDWithError(error: Error?) -> MBProgressHUD {
return showHUD(title: error?.localizedDescription, duration: HUD_Duration_Normal)
}
@discardableResult
func showWaitHUD(title: String?) -> MBProgressHUD {
let HUD = showHUD(title: "", detail: title, duration: TimeInterval(HUD_Duration_Infinite))
HUD.mode = .indeterminate
HUD.isUserInteractionEnabled = true
return HUD
}
@discardableResult
func showHUD(title: String?) -> MBProgressHUD {
return showHUD(title: title, duration: HUD_Duration_Normal)
}
@discardableResult
func showHUD(title: String?, detail: String?) -> MBProgressHUD {
return showHUD(title: title, detail: detail, duration: HUD_Duration_Normal)
}
@discardableResult
func showHUD(title: String?, duration: TimeInterval) -> MBProgressHUD {
return showHUD(title: "", detail: title, duration: HUD_Duration_Normal)
}
@discardableResult
func showHUD(title: String?, detail: String?, duration: TimeInterval) -> MBProgressHUD {
let HUD = MBProgressHUD.showAdded(view: self, duration: duration, withText: title, animated: true)
HUD.mode = .text
HUD.detailsLabel.text = detail
return HUD
}
@discardableResult
func showHUD(title: String, error: Error) -> MBProgressHUD {
let errorDetail = error.localizedDescription
return showHUD(title: title, detail: errorDetail)
}
@discardableResult
func showHUD(title: String, detail: String,iconImage: UIImage, duration: TimeInterval) -> MBProgressHUD {
let HUD = showHUD(title: title, detail: detail, duration: duration)
HUD.mode = .customView
HUD.customView = UIImageView(image: iconImage)
return HUD
}
func hideHUD() {
MBProgressHUD.hide(for: self, animated: true)
}
@discardableResult
func currentHUD() -> MBProgressHUD {
return MBProgressHUD(view: self)
}
}
iOS:swift MBProgressHUD
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 你能想象弹框闪退了?原谅我知道的少哈~~~ 代码修改如下: 参考链接: https://blog.csdn.net...
- 代码很简单,罗列了三种类型,分别是默认的菊花提示、纯文本提示、自定义视图提示。默认提示: 效果图: 纯文本提示: ...
- 前言: MBProgressHUD,是iOS开发中很经典的一个框架,我们可能每个项目,乃至每个ViewContro...
- References:http://devonios.com/use-mbprogresshud-in-swift...
- 之前给大家分享过系统的中间弹框,底部弹框UIAlertController,今天来补充一下MBProgressHU...