📜 ⬆️ ⬇️

Manual layout vs Storyboard / Nib

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:

Storyboard and xib
')
Problems:
  1. Most of the code associated with the animations is not there to put.
  2. 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).
  3. 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:
  1. The layout is visual.
  2. User-story is visible when viewing.
  3. Sizeclasses
  4. Use Embedded Segue / Storyboard Reference and live happily
  5. 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 Autolayouts

I used various tula, the most convenient - PureLayout and Cartography (for swift).

Problems:
  1. More code (approximately 1.2 times).
  2. No visibility
  3. Difficult layout turns into hell

Pros:
  1. A little faster - we save on the NSKyedArchiver file Xib / Storyboard parsing.
  2. Declarative and all in one place (no need to constantly switch between Storyboard and text).
  3. It is more convenient to do animations (for example, pan).

Manual Layout

Problems:
  1. If implemented incorrectly, there are hardcoded values ​​(if correct, only padding, etc., and this can be put in separate headers).
  2. More code (approximately 1.5 times).
  3. No visibility
  4. Themselves need to take care of adequately changing the frame, and when changing the parameters of the external view
  5. No SizeClasses

Pros:
  1. With proper implementation, everything is very flexible.
  2. It works faster.
  3. You can calculate the frames in the background-stream and simply apply them later.

Total

If 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:

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:


Manual layout is used where it is needed, and we switch to frames - if autolayout becomes a bottleneck (see the mish comment ).

Source: https://habr.com/ru/post/271355/


All Articles