Menu

Developer / Designer Workflow

04 January 2018

(You can find the Xcode project and the Sketch file for this post here.)

There are two approaches to building user interfaces: using a visual tool or doing everything in code. Each has its own advantages and disadvantages. Let's see what they are and find a way to combine the best of both when designing and building an iOS app.

Visual Tools vs Coding

The biggest advantage of a visual tool is that if you know the tool well, building the UI is usually very fast -- you select the elements that you need from the available set, arrange them on the screen, adjust some properties, and you are done. Another advantage is that you see what the UI will look like while you are working on it, so you can iterate quickly. For iOS, Interface Builder has a large selection of available UI elements -- almost any UI element that UIKit offers has an Interface Builder element that you can drag and configure. You can express the layout via constraints visually, and IB gives you a preview of what it would look like on different screens and even warns about ambiguous or impossible layouts. Storyboards allow you to express multiple screens in one place and describe the flow in the app visually. Actions and gesture recognizers allow you to express user interactions.

If you want to build a very basic iOS app that has very few screens and uses only iOS standard visual elements, this approach works well, but if you are building an app that uses a particular visual style and has more than a few screens, this approach becomes very error prone. Any change in style (for example, changing a font) requires looking through all XIB files to update elements that may have changed and manually update them. Rearranging the app flow (for example, changing the order of some screens) also becomes very error prone -- in addition to changing the storyboard, the code for segues also needs updates, and because segues are dynamic, the compiler can do very little to ensure that the code changes match the visual changes in storyboards.

The other extreme is to do everything in code. The main advantage there is that it becomes possible to build reusable code so that changes in style or app flow can be isolated and applied consistently and with much greater help from the compiler. In addition to greater control, coding everything also results in much better understanding of the APIs and how the app really works. Unfortunately, it often comes at the price of far greater code complexity, so the app is more difficult to maintain, especially for people who are new to the code. Coding is also much slower than dragging already made UI elements and adjusting their properties in inspectors.

The Middle Ground

Ideally, we'd like to combine the simplicity and speed of the visual tool approach with the reusability of the coding approach. One way to do this is to code everything related to visual elements and turn visual elements into IBDesignables. Then we can still use Interface Builder to position and layout elements on the screen with immediate preview of how the screen would really look but without repeatedly setting the same properties to achieve the same look across the app. Another way is to do something similar with UIAppearance or third party tools like Classy to style components at runtime. The advantage of coding IBDesignables is that you see the actual design in Interface Builder and don't have to run the app. You also get much more compiler / IDE help as you write the styling code.

Designing app screens involves solving the same problem of keeping screens consistent, and the solution is very similar too. For example, Sketch has symbols -- reusable objects that you make once and then reuse everywhere. Recently, Sketch introduced library symbols that share groups of symbols across designs, which could be useful for styling more than one app in the same way. Building those symbols in code and packaging that code as a framework provides a similar library for the actual app or a group of apps.

Building Reusable Components

The most basic building blocks for app components are colors and fonts. Here is an example of how the code for common colors might look:

This approach scales well for different components and styles and makes it much easier to have a consistent design throughout the app while retaining much of the convenience of laying out the UI visually. This applies equally well to Xcode project content and Sketch files, as well as keeping the design and the implementation in sync. Beyond a single app, components can be further reused in multiple apps, which is especially valuable to convey the same design for branding.

Just like Sketch recently introduced symbol libraries for symbols that are reusable across Sketch files, components can be packaged as frameworks. This works well if the code depends only on UIKit / Foundation. If there are any other dependencies, it may be more practical to share the source code.