Apple’s new state driven declarative UI Framework

iOS13 is the minimum deployment version for SwiftUI.

Transition from traditional UI development to SwiftUI is reasonably smooth even with existing UIKit applications.

Swift UI — the framework

In Apple’s words — Swift UI provides an exceptionally simple and fast way to build user interfaces across all Apple platforms with the power of Swift.

It is a framework for creating user interfaces that dramatically reduces the amount of boiler plate code required to create UIs. A good UI experience is built on top of UI components that adjusts to different device sizes and types. Swift UI enables you as a developer to take this experience one notch higher with minimal effort since it provides automatic support for Dynamic Type, Dark Mode, Localization, and Accessibility out of the box.

Declarative UI: define WHAT not the HOW.

By very definition — Declarative UI lets the developer define what he/she wants to create rather than how to create it.

With Declarative UI we declare UI in terms of its hierarchy using reusable components. The following image from apple provides some context into it:

Declarative UI, Hierarchy of components. Source — Apple
Code to view hierarchy

This works totally opposite to the traditional imperative way of laying out UI where developers are required to provide absolutely every single layout property and constraints.

Swift UI allows developers to define the type of UI they want and trust the framework to render a usable result.

Swift UI vs traditional Storyboards

The declarative UI style very closely mimics the actual view hierarchy. Let’s have a look at the following example:

import SwiftUI struct ContentView: View { 
var body: some View {
Text(“Turtle Rock”)
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
Example from Apple’s tutorial
Storyboard code for the same layout

Out of the Box Advantages with Swift UI

Swift UI translates your code into different appearances based on the context that the UI is presented in. While writing controls like buttons, labels, sliders etc. Swift UI automatically alters the appearance based on the device and the context of these controls so that these controls then seamlessly work with features such as dark mode.

Swift UI allows developers to write descriptive code that describes actual purpose of the code rather than worrying about appearance in different environments.

Swift UI provides complete support to enable accessibility with custom controls in order to assist specially enabled users. This includes Voice Over. A well described control will work straight out of the box.

Author of Reactive programming with Swift, Senior Software Engineer — Exploring possibilities with new Tech.

Get the Medium app