[英]XIB file not loading from framework
我創建了可可觸摸框架,在其中創建了UIViewController
類,該類在xib
文件中xib
ui。 我已經完成了xib和控制器文件(例如連接到控制器類的文件所有者)之間的必需綁定。
我已經在單獨的項目中創建了另一個xib和控制器文件,作為普通的ios應用程序。 在那里,我試圖呈現此ViewController,它將出現在屏幕上,但是當我嘗試加載框架中存在的控制器時,只有黑屏出現在屏幕上。
我在xcode的build phases
copy bundle resources
添加了xib
文件。 但是當我在本地Pod安裝框架時,我只能看到控制器文件以及其他swift
文件,而看不到xib
文件。
let oPController = OPController()
let oNav = UINavigationController(rootViewController: oPController)
controller.present(oNav, animated: true, completion: nil)
當框架包含xib
文件時,需要特別注意嗎?
注意當我查看項目中已安裝的Pod時,那里看不到任何xib文件。
嘗試將其添加到podspec文件
s.source_files = "ReusableViewController/*.{swift,h,m,xib,storyboard}"
s.resource_bundles = {
'MyFramework' => ['Pod/Classes/**/*.{storyboard,xib,xcassets,json,imageset,png}']
}
s.exclude_files = "Classes/Exclude"
s.swift_version = "4.0"
檢查此鏈接以從框架加載您的xib文件
https://github.com/Ajithram007/reusableVC
let nibName: String = "SignInView"
var view: UIView!
public override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
self.view = UINib(nibName: self.nibName, bundle: Bundle(for: type(of: self))).instantiate(withOwner: self, options: nil)[0] as! UIView
self.view.frame = bounds
self.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(self.view)
}
在框架中使用xib創建任何ViewController時。 您必須做這些事。
如果使用cocoapod庫,請確保已將xib
添加為podspec文件中的資源。
根據Sulthan的回答並進行了測試,當使用xib動態加載任何UIViewController
類且某個類存在於某個框架中時,則必須在動態加載類時顯式提供bundle的上下文
像這樣打電話
let oPController = OPController(nibName: "OPController", bundle:
Bundle(for:OPController.self))
這個班級看起來像
class OPController : UIViewController {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.