【发布时间】:2020-08-11 15:36:42
【问题描述】:
我正在为自定义 toast 开发我的第一个 pod 文件。我在 .xib 文件中设计了 UIView。 这在初始项目中工作正常,但是当我将这些文件复制到 pod 项目时,它显示
线程 1:异常:“无法在包中加载 NIB:'NSBundle /sameernawaz/Library/Developer/CoreSimulator/Devices/9FD32443-4431-480F-93B3-E1106D74E944/data/Containers/Bundle /Application/05EFC4C0-A60C-4A72-B23D-8575F0218B64/MotionToastView_Example.app>(已加载)',名称为 'MTVibrant'"
我尝试添加这些 .xib 文件目标 -> 复制捆绑资源,但没有任何帮助。我不确定我是否允许使用 xib 文件,或者我是否应该在 Swift 中编写 UIView。
import UIKit
class MTVibrant: UIView {
@IBOutlet weak var headLabel: UILabel!
@IBOutlet weak var msgLabel: UILabel!
@IBOutlet weak var circleImg: UIImageView!
@IBOutlet weak var toastView: UIView!
@IBOutlet weak var circleView: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() {
let viewFromXib = Bundle.main.loadNibNamed("MTVibrant", owner: self, options: nil)![0] as! UIView
viewFromXib.frame = self.bounds
addSubview(viewFromXib)
}
}
【问题讨论】:
-
Bundle.main,这就是你的错误所在。您的视图位于 Pod 包中,而不是您的主应用包中。 stackoverflow.com/questions/44086009/… -
这能回答你的问题吗? Path to bundle of iOS framework
-
但它仍然由我自己的 pod 文件调用,而不是由最终项目调用。开发人员调用我的 ToastMethod,它会显示出来。他传递参数,一旦他调用它,它就会进入我的 pod ExtensionFile 并调用这个 UIView。或者我这样做对吗?
window.addSubview(toastView!) UIView.animate(withDuration: 1.0, delay: toastDuration, animations: { toastView!.alpha = 0 }) { (_) in toastView!.removeFromSuperview() }此代码在 pod 内。 toastView就是上面的UIView
标签: ios swift xcode cocoapods bundle