Most iOS developers are familiar with Storyboard. Apple suggests using it. But lately, the convenience of this tool makes me doubt.
Below, I want to compare different methodologies using a Storyboard or a set of Xib files, with a manual verst (using and without autolayout). I do not pretend to the completeness of the disclosure of the topic and I will be glad if you point out errors to me and / or suggest other methodologies and comparison criteria.
The post does not contain experiments, academic calculations and is based on my knowledge and experience in the field of iOS development. I would be glad to know the opinions of experts!
With certain reservations, there are the following development methodologies:
- One Storyboard and everything in it (minimum layout code in the code) - used in small projects; very inconvenient when developing by the development team.
- Several Storyobard-s and all in them.
- We forget about Storybaord, we place all UI controls in xib files (see this post ).
- Forget about Storyboard and Xib, but use autolayout - hi PureLayouts.
- We forget about all Storyboard / Xib / AutoLayouts and set up all the View frames manually.
- We forget about UIKit - use third-party AsyncDisplayKit solutions from Facebook or ComponentsKit. (I have no such experience, so I won’t say anything worthwhile).
Storyboard and xib')
Problems:
- Most of the code associated with the animations is not there to put.
If there are several of them, then when switching from one to another, a load is created on the main thread (NSKeyedArchiver must parse the storyboard, and it itself is slow).You can not do everything in Storybaord with all the desire. For example, set cornerRadius, shadowOffset, etc. ; in fact, you can - through user defined runtime attributes (@IBInspectable)
Pros:
- The layout is visual.
- User-story is visible when viewing.
- Sizeclasses
- Use Embedded Segue / Storyboard Reference and live happily
- We abstract from the type of transition and to implement the transition itself, it is enough to know the segue identifier (see RamblerSegues ), also, when using automatic dependency injection libraries (see typhoon), we abstract away from dependency injection.
Manual Layout with AutolayoutsI used various tula, the most convenient - PureLayout and Cartography (for swift).
Problems:
- More code (approximately 1.2 times).
- No visibility
- Difficult layout turns into hell
Pros:
A little faster - we save on the NSKyedArchiver file Xib / Storyboard parsing.- Declarative and all in one place (no need to constantly switch between Storyboard and text).
- It is more convenient to do animations (for example, pan).
Manual LayoutProblems:
- If implemented incorrectly, there are hardcoded values ​​(if correct, only padding, etc., and this can be put in separate headers).
- More code (approximately 1.5 times).
- No visibility
- Themselves need to take care of adequately changing the frame, and when changing the parameters of the external view
- No SizeClasses
Pros:
- With proper implementation, everything is very flexible.
- It works faster.
- You can calculate the frames in the background-stream and simply apply them later.
TotalIf you reject each of [Stobyboard, Xib, Autolayout], the work becomes more complicated and the code becomes more.
It makes sense not to use the Storyboard (not necessarily fully) if:
- We make complex animations (there is no need to throw a bunch of outlets).
- Recovery time from the archive becomes critical.
- There are many crutches (usually, most of them are eliminated by the correct architecture and / or method swizzling).
It makes sense that the resource-demanding TableView / CollectionView is not done in either the Storyboard or Xib (we lose time parsing the file, we lose
flexibility ). When optimizing, you can first impose using autolayouts, but if the lags do not pass - measure in the tools, make sure that the layout-s slow down, and then go to the manual account.
Sometimes, similar content needs to be displayed in both TableViewCell and CollectionViewCell. With manual layout this will not be a problem. And when using Xib, it is solved, for example, as follows: type the contents of the cell in the xib file, and in init-e cells call addSubview with this view.
EDIT:Summarizing, we can say that the Storyboard has the following advantages over manual layout:
- Visualization (layout in the code is often “understandable” only to its author - see the comment by DmitrySpb79 ).
- Using multiple Storyboard allows you to separate different user-stories and reduces the likelihood of conflicts during team development.
- If you need to use a controller in one of the storyboards, from another storyboard - from iOS 8 we can use the Storyboard Reference and this comment from visput ; if you need to support iOS versions <8, then you can use the Xibs, or you can somehow implement an analog of Storyboard Reference
Manual layout is used where it is needed, and we switch to frames - if autolayout becomes a bottleneck (see the
mish comment ).