MBProgressHUD不能在swift中工作:不能导入和使用



我使用cocoapods来安装MBProgressHUB,在桥接头中我不能只做

 #import "MBProgressHUD.h"

I changed to

#import "MBProgressHUD/MBProgressHUD.h"

导入是OK的,但我不能在swift代码中使用它?我做错什么了吗?我怎样才能解决这个问题?

试试这个:

1)在Podfile中指定use_frameworks!使用框架(而不是静态库)。

在添加用Swift编写的pod作为依赖时是必需的,如果你的应用是用Swift编写的,这通常是一个好主意。

2) Do pod install

这将确保您的项目设置为实际使用上述内容。

3)在桥接头中添加#import <MBProgressHUD/MBProgressHUD.h>(注意尖括号-不是引号)和需要使用它的Swift类中的import MBProgressHUD

,

MyApp-Bridging-Header.h:

#import <MBProgressHUD/MBProgressHUD.h>
// ... other imports ...

这将把Objective-C文件暴露给Swift。尖括号表示这实际上是在导入一个框架。

MyViewController.swift:

import UIKit
import MBProgressHUD
// ... other imports...
class MyViewController: UIViewController {
  // ... yada yada...
}

这实际上是导入依赖项供你的视图控制器使用。

在添加Pod和桥接文件后,你可以在Swift 3中直接使用。

  var hud = MBProgressHUD()
  hud = MBProgressHUD.showAdded(to: navigationController?.view, animated: 
  true)
  // Set the custom view mode to show any view.
  hud.mode = MBProgressHUDModeCustomView
  // Set an image view with a checkmark.
  let gifmanager = SwiftyGifManager(memoryLimit:20)
  let gif = UIImage(gifName: "miniballs1.gif")
  let imageview = UIImageView(gifImage: gif, manager: gifmanager)
  hud.labelText = NSLocalizedString("Loading", comment: "")
  hud.labelColor = UIColor.red
  imageview.frame = CGRect(x: 0 , y: 0, width: 25 , height: 25)
  hud.customView = imageview
  // Looks a bit nicer if we make it square.
  hud.show(true)
You can directly drag MBProgressHUD Folder to your swift project, It will create the Bridging header as named "YourAppName-Bridging-Header.h", it means you can import obj-c classes into your swift project.

'import UIKit
import MBProgressHUD
class MyViewController: UIViewController {
  // write your code
}'
This actually imports the dependency for use by your view controller.

创建扩展以易于使用和贯穿整个应用程序

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)
        }
    }
}

用法:

1。SHOW - self。showHUD (progressLabel:"加载…")

2。HIDE - self。dismissHUD (isAnimated:真)

最新更新