【发布时间】:2015-08-25 01:41:16
【问题描述】:
我使用 cocoapods 安装 MBProgressHUB 并且在桥接头中我不能这样做
#import "MBProgressHUD.h"
我改成
#import "MBProgressHUD/MBProgressHUD.h"
导入没问题,但我不能在 swift 代码中使用它?我做错什么了吗?我该如何解决这个问题?
【问题讨论】:
标签: ios swift mbprogresshud
我使用 cocoapods 安装 MBProgressHUB 并且在桥接头中我不能这样做
#import "MBProgressHUD.h"
我改成
#import "MBProgressHUD/MBProgressHUD.h"
导入没问题,但我不能在 swift 代码中使用它?我做错什么了吗?我该如何解决这个问题?
【问题讨论】:
标签: ios swift mbprogresshud
试试这个:
1) 在Podfile 到use frameworks 中指定use_frameworks!(而不是静态库)。
这是添加以 Swift 编写的 pod 作为依赖项所必需的,如果您的应用程序是用 Swift 编写的,这通常是一个好主意。
2) 做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...
}
这实际上会导入依赖项以供您的视图控制器使用。
【讨论】:
在 Swift 3 中添加 Pod 和桥接文件后可以直接这样使用。
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: "Loading...")
2。隐藏 - self.dismissHUD(isAnimated: true)
【讨论】: