I want to create a specific xib for every screen size, but with one UIViewController.
How can I connect few xibs to one class?
MyClass is your UIViewController
1. Create different xibs
myclass_iphone4
myclass_iphone5
myclass_iphone6
2. Each xib will have as file's owner MyClass to link the outlets
3. Load MyClass dynamically:
let deviceSize = //iphone4, iphone5, ....
let viewcontroller = MyClass(nibName: "myclass_\(deviceSize)", bundle: nil)
Let me know in the comments if it could be ok for you.
Related
Created the following files (view with xib and view controller):
SomeViewController.swift
SomeView.swift
SomeView.xib
NOTE: The names may differ but the class names should look similar as in this example.
SomeViewController is just a subclass of UIViewController and is not linked with SomeView at all.
But when I use SomeViewController it somehow calls awakeFromNib() in SomeView and it causes crash. Checked twice - the bug won't appear if I for example change class name from SomeViewController to SomeViewController2.
Code for my case:
let vc = SomeViewController()
... //add it to UITabBarController
Must I rename it to something fully different or can I somehow fix this link between view and view controller?
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621487-nibname
If the view controller class name ends with the word ‘Controller’, as
in MyViewController, it looks for a nib file whose name matches the
class name without the word ‘Controller’, as in MyView.nib.
Solution:
class SomeViewController: UIViewController {
override var nibName: String? {
return nil
}
}
This question already has answers here:
Loading UIViewController "from" Nib File
(2 answers)
Closed 5 years ago.
I have 2 storyboards, I have common screen for both storyboards. I have created it as a separate view controller with xib. I want to load it in between storyboard screens.
You can load VC from Nib file very easily. try the code below :
func presentMyViewController() {
let myVC = MainViewController(nibName:"MyViewController", bundle:nil)
self.navigationController.pushViewController(myVC, true);
// you could present it another way, such as:
// self.presentViewController(myVC, true, nil)
}
Edit : As per comment I don't think that we can directly load xib from story-board but if we need to navigate to xib then you can try above code or else you can also add child view controller directly on viewcontroller.
Something like below:
You can override the loadview() method in class.
public class MyViewController {
override func loadView() {
Bundle.main.loadNibNamed("MyViewController", owner: self, options: [:])
}
//your stuff here...
}
Assumes of course your XIB is MyViewController.xib. Adjust to suit.
edit: also assumes you've correctly set up the files owner references in the XIB (i.e. to the view controller in question).
I'm trying to implement a page menu in my app using the pod from this pre-built page menu pod from github
In the instructions, it says:
var controller : UIViewController = UIViewController(nibName:"controllerNibName", bundle: nil)
controller.title = "SAMPLE TITLE"
controllerArray.append(controller)
things are all set up but i need only "controllerNibName". I am not using storyboard. And haven't made any xib file yet with viewController. so how can i get the NibName of viewController.
controllerNibName string is a file name of your XIB. Where you have done all the UI which is related with your UIViewController.
If your viewController class name and xib file names are same then you can directly allocate the UIViewController. It will pick the xib automatically.
Assume UIViewController name is PageOne & xib name also should be PageOne.xib
let pageOne = PageOne()
if your xib name is different from your view controller name
For Example : Page1.xib
let pageOne = PageOne(nibName:"Page1", bundle: nil)
There is a nice way to get a name of nib (if nib name is equal to controller name i.e. MyViewController.xib and MyViewController):
let controllerName = String(describing: MyViewController.self)
var controller : MyViewController = MyViewController(nibName:controllerName, bundle: nil)
I am struggling to figure out how to load a xib from within a storyboard using Swift in XCode.
My main storyboard (Called TabBarNav.storyboard) is a Tab Bar View controller with 4 items (Home, Weight, Meals and Calories).
I have created a seperate XIB UIView called ViewWelcome.xib with corresponding class file ViewWelcome.swift.
The Home items view controller has a class file called "ViewControllerHome.swift"
When the Home tab bar item is touched I want to replace the existing view with the one within ViewWelcome.xib.
In ViewWelcome.xib I have made the files owner ViewControllerHome but when I run the app it is still showing the view that was created with the Home item when I originally created the storyboard.
Reason I am doing it this way: (A Mix of storyboard and xibs) I wanted each section (Home, Weight, Meals and Calories) to be seperate from the storyboard so that the SB doesnt become cluttered and to also later avoid merge issues in git when more than one person works on interface files.
Simplest way is to create & add your nib view in viewDidLoad
func viewDidLoad() {
super.viewDidLoad()
// create and add view from nib
if let viewWelcome = UINib(nibName: " ViewWelcome", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as? ViewWelcome {
viewWelcome.frame.size = self.view.size
self.view.addSubview(viewWelcome)
}
}
Otherwise refer to this
This is working for me . Give a try . .
let newView = Bundle.main.loadNibNamed("ViewWelcome", owner: self, options: nil)?.first as! ViewWelcome
self.view = newView
I have a UIViewController that looks a bit like this:
class ProfileViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, ... {
...
convenience init(name: String) {
print("Init with student: \(name)")
self.init()
}
...
}
I have a corresponding Storyboard layout for this, embedded in a UINavigationViewController, linked to a UITabBarController. This seemed like the easiest way to design the layout, and is great for when there's only one instance of this VC required (which is how the app was originally designed).
I'd now like to create multiple tabs from this single design (between 1 and 3), and pass the VC init information programatically, but I'm unsure exactly of the best way to do this - as you can see above I've added a convenience init function based on some reading I've done as that seemed like a good place to start.
It's easy enough to create new named tabs based on the storyboard layout like this:
for user in (users)! {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "profileNC")
controller.tabBarItem.title = user.name
newTabs?.insert(controller, at: 0)
}
self.viewControllers = newTabs
But, doing it this way I don't get to call the init function to pass the UIViewController the information it needs to display correctly.
How can I create my ViewControllers, link the layout to the Storyboard and use the custom init function? Is this even possible?
Any suggestions gratefully appreciated!!
When using a storyboard you cannot use a custom initialiser. You will need to either set the property directly or use a configuration function on the view controller instance after you have created it.