I have created cocoa touch framework where I have created UIViewController
class which has the ui in xib
file. I have done required binding between xib and controller files like file owner connecting to controller class.
I have created another xib and controller files in separate project as normal ios app. There I tried to present this ViewController it was coming on to the screen but when I tried to load the controller present in framework only black screen was coming up to screen.
I have added xib
files in copy bundle resources
part of build phases
in xcode. But when I pod install the framework locally I am able to see only controller file along with other swift
files but not the xib
ones.
let oPController = OPController()
let oNav = UINavigationController(rootViewController: oPController)
controller.present(oNav, animated: true, completion: nil)
Is there any special care to be taken when framework contains xib
files?
Note When I look in the installed pods in project I cannot see any xib files there.
Try adding this to the podspec file
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"
Check this link to load your xib file from framework
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)
}
When you are creating any ViewControllers using xib's in your framework. You must do these things.
Make sure you have added xib
as resources in podspec file in case of cocoapod library.
As per Sulthan's answer and which I tested, When any UIViewController
class is loaded dynamically using xib and this class is present in some framework then you have to explicitly give the context of bundle while dynamically loading the class
Call like this
let oPController = OPController(nibName: "OPController", bundle:
Bundle(for:OPController.self))
where this class looks like
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")
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.