0

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.

4
  • 2
    You have to specify the Bundle to load xib from. When you use init without parameters, it will use Bundle.main which is not the correct one in this case. Of course, the framework bundle has to be copied correctly during the build. Commented May 14, 2019 at 7:12
  • where I have to do this? Commented May 14, 2019 at 7:14
  • Is it ok to have xib files in copy bundle resources ? Commented May 14, 2019 at 7:16
  • You should try OPController(nibName: nil, bundle: Bundle(for: OPController.self) first. That should make sure the xib is loaded from the correct bundle. Commented May 14, 2019 at 7:18

2 Answers 2

2

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) } 
Sign up to request clarification or add additional context in comments.

Comments

1

When you are creating any ViewControllers using xib's in your framework. You must do these things.

  1. Make sure you have added xib as resources in podspec file in case of cocoapod library.

  2. 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") } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.