Have you ever felt difficulty while choosing between Storyboard and XIB or even going for Custom code? Let’s make it easier from this short article.
As we know, Storyboard introduced in iOS 5 and it consists of several ViewControllers, whereas XIB (previously NIB) files can consist of only one controller scene. Even custom codes can do all UI actions of such as positioning or animations..etc without help of any GUI tools. Further, we may not need to stick with only one of above options. We can have one or combinations of three as per the requirement.
Pros of Storyboard:
- As its name says, we can visualize entire scene in a single shot.
- On the Storyboard file, we can configure the flow between pages directly by using segue. Segues takes care most of UI configurations.
- Reduces boilerplate code needed to pop, push, present and dismiss view controllers.
When storyboard makes sense?
- A set of views say for example: Authentications (Login/Registration page), Wizards like welcome scene or tutorial for an app etc
- Master details set of views for example: Profile lists and profile details.
- Prototyping table view with multiple static table view cells. Since it can be clubbed inside one parent table view.
Cons of Storyboard:
- Complicating team environment: when multiple developers work on the same storyboard file at the same time, source control conflicts are inevitable.
- We also observed in larger storyboard file, it takes time while opening and performing certain changes.
Pros of XIB:
- Breaks a view controllers view into separate modules. So that each component is easy to develop, debug and test.
- Lazily loaded. So they don’t occupy memory unless needed.
So, when XIB makes sense?
- Reusable view template.
- Reusable table view cells.
Cons of XIB:
- Views with dynamic content, where layout changes depending on the content.
- Views that are not easily design able in Interface Builder.
- Views that has complicated transitions and can be simplified with Storyboarding.
Taking time to write Custom code for UI ?
- When we need render dynamic (loaded based on the content) UI which cannot be achieved from Storyboards or XIB.
- Customizing views such as Rounded corners, Shadows or for dynamic frame sizes.
- Developer will have clear idea of rendered UI components.
But ...
Writing custom codes are never be a bad idea. But can lead to boiler plate codes and in long term, may lead maintenance challenges.
Conclusion :
- Apple strongly believes that storyboards are future. It simplifies designing process and code size. Ultimately it leads lesser bugs. But keep in mind that, larger storyboard is not recommended.
- Unlike storyboard, XIBs are flexible and focused on single view with no visual flow.
- If none of storyboard or xib gives advantage, write code.
Further Reading :
Debate from Raywenderlich & team gives many for dimensions on this topic.
Also refer,