Exploring SwiftUI - A Broad Overview 1/4: SwiftUI vs UIKit

This article is the 1st of a series of 4 aimed at exploring the capabilities of SwiftUI. Happy reading!

Razvan Bogdan
XapoLabs
3 min readApr 18, 2023

--

Photo by James Harrison on Unsplash

Intro

SwiftUI has been around since 2019 already. It was first introduced by Apple with iOS13 and is built to be less verbose and intuitive than its UIKit counterpart.

It had a slow start, lacking a lot of functionalities which were already available in UIKit. One could say, it was released as a PoC. But slowly, it grew mature, and more and more projects are adopting it. With every release of iOS, Apple enriches SwiftUI’s capabilities by providing new functionality.

SwiftUI vs UIKit

Let’s explore some benefits of using SwiftUI over UIKit:

  • its declarative syntax is more robust than UIKit’s imperative approach, allowing developers to define the desired outcome of a UI element, rather than detailing the exact steps needed to create that element;
  • its two-way-binding approach, where the UI gets updated automatically in response to data changes;
  • the code is more readable and easier to understand. Gone are the days when solving conflicts in .xib files or in programmatically-written UI were a thing;
  • allows for faster and more efficient development and debugging. Building UI using SwiftUI requires less code as it provides automatic layout management, reducing the risk for errors;
  • makes it easier to create and make use of custom reusable UI, also with the help of Live Previews which speed up the feedback process;

Example

But it’s always better to have a visual example. For the sake of this exercise, let’s create a simple form containing two text fields and a button, without any logic. We will create the same UI in both UIKit and in SwiftUI and compare the results.

The user interface we will be building.

Here’s how this UI would be built programatically in UIKit:

And this is how it would be built in SwiftUI:

Notice in the UIKit example the need to build element hierarchies manually and setup constraints using a really cumbersome way. SwiftUI takes away all that pain and adds the declarative sintactic sugar that makes it more efficient to implement and change UI related code.

Even more, notice the number of lines needed to create the ui in UIKit (31), compared to SwiftUI (13). And this is just a small example; in larger, more complex user interfaces the difference becomes even more noticeable.

Conclusion

As the SwiftUI framework is getting more and more mature and its community continues to expand, it is increasingly preferred as the choice of building the UI in iOS applications. Its growing popularity and versatility have led to its adoption by startups, scale-ups, and even bit-tech.

Thank you for reading! Got a suggestion? Please leave it in the comments section, feedback is always appreciated!

Let’s connect on LinkedIn!

If you’re interested in learning more about Xapo Bank — Bitcoin Bank providing simple and safe usability to retail customers, explore the resources at xapobank.com.

--

--