Summary
Nibs/.xib files and Storyboards both are Interface Builder files which are used to visually create user interface for iOS and Mac applications in Xcode (i'll use iOS terminology for classes as this question is tagged iOS but it also applies to Mac programming).
Differences
Nibs are intended to be used with a single UIView
. They can also be connected to a UIViewController
subclass by settings the class of File's Owner to any subclass of UIViewController
and connection the view outlet (drag to connect using the Connections Inspector in the far right pane of Xcode).
Storyboards are intended to contain the user interface for 1 or more UIViewController
. You can build your entire user interface in a single storyboard or separate it into smaller parts.
Advantages
Storyboards should always be used in favor of .xib files/Nibs (for view controllers). Storyboards have more features and are actively developed by Apple.
Every argument in favor of Nibs rely of the fact that they used individually while storyboards contain many scenes. You can use a single storyboard for each UIViewController
just as easily as you can with Nibs (see code samples below). Keep reading for a detailed explanation and code examples.
Detailed
Why are Storboards superior to Nibs?
The answer basically comes down to Apple encouraging the use of Storyboards and putting more development effort into them.
- Storyboards have zooming capability which Nibs lack. Seriously, you can't zoom at all in Nibs which sucks when designing for bigger screens on a small laptop.
- Nibs are missing key functionality like:
- Prototype and dynamic cells for
UITableView
(more info)
- The top layout guide property (see comment)
- There are probably more, please edit or comment if you have something to add to this list
- You don't need to mess with setting the class of Files Owner.
The basic argument against storyboards is that having all your view controllers in one place leads to merge conflicts, a slow Xcode, slow build times and being a general pain in the butt to maintain. Hence, general advice is to use a Nib for each UIViewController
.
But... You can just create storyboard for each UIViewController
. A common practice (for me at least) is to hide all the UIViewController initialization in a class method (as no other class needs to know the name of the file where the controller's Nib/Storyboard is located).
Lets compare the related code snippets that one might use to create such a method. A single line of code is the entire difference between the two.
Objective-C
Storyboard
+ (ViewController *)create
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"ViewController" bundle:nil];
return [storyboard instantiateInitialViewController];
}
Nib
+ (ViewController *)create
{
return [super initWithNibName:@"ViewController" bundle:nil];
}
Usage
- (void)showMyViewController
{
ViewController *vc = [ViewController create];
[self presentViewController:vc animated:YES completion:nil];
}
Swift
Storyboard
static func create() -> ViewController {
let storyboard = UIStoryboard(name: "ViewController", bundle: NSBundle.mainBundle())
return storyboard.instantiateInitialViewController() as! ViewController
}
Nib
static func create() -> ViewController {
return ViewController(nibName: "ViewController", bundle: nil)
}
Usage
func showMyViewController() {
let vc = ViewController.create()
self.presentViewController(vc, animated: true, completion: nil)
}
Arguments
I'll address all the usual arguments for Nibs; as I mentioned earlier, there are mostly in favor of single files, not as an argument for Nibs over Storyboards
- Teams and merging
Argument: Having a storyboard with lots of view controllers will
cause merge conflicts if you are working on a team with multiple
people making changes
Response: A single storyboard causes no more merge conflicts than a single Nib
- Complexity
Argument: Very complex apps have a lot of scenes in the Storyboard which leads to a giant storyboard that takes forever to load and is barely comprehensible because of it's size.
Response: This is a great point, but you can easily break Storyboards into smaller parts. Storyboard References look like a great feature that can be used to link Storyboards together but they are only available in Xcode 7/iOS 9+. Also, still not a reason to choose individual Nibs over Storyboards.
- Reuseability
Argument: Creating a Nib for each UIViewController
subclass lets you reuse code so you don't have to setup all your constraints and outlets for each scene in your storyboard.
Response: Again, not a reason to choose individual Nibs over individual Storyboards.