How to Choose Between SwiftUI and UIKit.

Gopal Sorathiya
Globant
Published in
6 min readJun 8, 2022
Image Source

World is rapidly changing and adopting new technologies with time. Apple has introduced swiftUI in iOS 13 release.we are still using UIKit for developing iOS applications, swiftUI is still in development stage apple is changing things day by day in swiftUI. Everyone has one question in mind, can SwiftUI overcome UIKit in the future? Answer is Yes let’s dive into comparison, advantages and drawbacks of swiftUI over UIKit.

UIKit

UIKit is Apple’s old framework which is used for developing iOS apps. It has Storyboards and XIBs for interface builder.

“The Interface Builder editor within Xcode makes it simple to design a full user interface without writing any code. Simply drag and drop windows, buttons, text fields, and other objects onto the design canvas to create a functioning user interface.”

This is what Apple’s documentation says about interface builder.

Let’s go through some advantages and drawbacks of UIKit :

Advantages:

  • It’s very easy to learn even for beginners.
  • With UIKit and Storyboard you can create prototypes which can be visualised on storyboard interfaces. Storyboards in UIKit can reduce code.
  • It has a large number of objects available so it’s easy to build any functions and tasks.
  • UIKit is more than 10 years old, so it’s very stable to use and it has a large number of features and community support.
  • Resources and materials can be easily available, so it can be adopted easily.

Drawbacks :

  • Source control is quite difficult, In most cases storyboards lead to conflicts and it contains xml source, in which it’s very difficult to resolve merge conflicts as most developers do not have command over xml code.
  • While creating an application you have to keep in mind compatibility among all devices and auto layout issues.
  • As the number of screens increases in application it’s very difficult to manage in a single storyboard.
  • Making UI through code in UIKit is also a lengthy process and it also requires writing a large amount of code which makes more complex code to understand.

SwiftUI

SwiftUI was released in iOS 13 release.

SwiftUI helps you build great-looking apps across all Apple platforms with the power of Swift — and as little code as possible. With SwiftUI, you can bring even better experiences to all users, on any Apple device, using just one set of tools and APIs.

This is what Apple’s documentation says about SwiftUI.

Let’s go through some advanced features introduced in Swift UI.

Advanced app experiences and tools

Enhance your apps with new features, such as improved list views, better search experiences, and support for control focus areas. And gain more control over lower-level drawing primitives with the new Canvas API, a modern, GPU-accelerated equivalent of drawRect.

Accessibility improvements

Speed up interactions by exposing the most relevant items on a screen in a simple list using the new Rotor API. The current accessibility focus state, such as the VoiceOver cursor, can now be read and even changed programmatically. And with the new Accessibility Representation API, your custom controls easily inherit full accessibility support from existing standard SwiftUI controls.

SwiftUI improvements on macOS

New performance and API availability improvements, including support for multicolumn tables, make your macOS apps even better.

Always-On Retina Display support

On Apple Watch Series 5 and later, the Always-On Retina Display allows watchOS apps to stay visible, even when the watch face is dimmed, making key information available at a glance.

Widgets for iPadOS

Now widgets can be placed anywhere on the Home screen and increased to a new, extra-large widget size.

SwiftUI has proven itself to be an absolute game changer in the world of iOS development due to the fact that SwiftUI offers syntax that is easy to understand and is structured in a way that makes sense.

Not only this, but SwiftUI is a much more powerful library. SwiftUI will only continue to evolve as a powerful language, as Apple continues to shift its focus away from UIKit.

SwiftUI takes everything Apple learned from UIKit, and provides developers with much better features that are unavailable in UIKit. One of these features includes creating even more powerful applications while at the same time using less code than UIKit.

Comparison of SwiftUI and UIKit :

Below are the major differences between SwiftUI and UIKit.

  • Declarative vs Imperative :

SwiftUI is a declarative framework but UIKit is an imperative framework.

Let’s take a look at imperative and declarative programming by the below code example.

Imperative (UIKit):

class ViewController: UIViewController, UITableViewDataSource {
private var tableView: UITableView!
var listArray: [ListItem] = [
ListItem(name: “List item — 1”),
ListItem(name: “List item — 2”)
]
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
private func setupTableView() {
tableView = UITableView()
tableView.dataSource = self
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
let topAndBottomMargins = CGFloat(16)
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.bottomAnchor, constant: topAndBottomMargins),
tableView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor)
])
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: “Cell”, for: indexPath) as! TableViewCell
cell.textLabel?.textColor = .gray
cell.textLabel?.text = listArray[indexPath.row].name
return cell
}
}

Declarative example (SwiftUI):

struct ContentView: View {
var listArray: [ListItem] = [
ListItem(name: “List item — 1”),
ListItem(name: “List item — 2”)
]
var body: some View {
List(listArray) { person in
Text(person.name)
.font(.subheadline)
.foregroundColor(.gray)
}
}
}

By this example we can clearly see that swift UI has less code than UIKit.with the SwiftUI the data can be automatically bonded with the UI elements, so we don’t need to track the state of the User Interface. As a programmer, all we need to do is to set up data elements with the SwiftUI components. SwiftUI takes over the responsibility of making sure that the screen reflects the description.

  • Performance :

swiftUI is much faster than UIKit because Swift UI views use struct instead of class. It also provides a mechanism of declarative programming and a whole Combine framework, which renders UI changes faster on screen.

  • Interface Building Techniques :

SwiftUI has no interface builder like Storyboard and Xibs, swift code itself describes layouts rather than xml files.

SwiftUI provides automatic live previews to view that makes development faster rather than checking applications on simulator every time.

There is no auto layout in swiftUI, There are no content layout related issues. It provides compatibility to all Apple devices.Dark mode can be easily handled in swiftUI.

Modifiers can help in customisation of UI elements. You can easily draw different shapes by a few lines of code, and animation on views also can be achieved easily.

  • Teamwork and code Maintenance :

As swiftUI has less code and no interface builders like storyboards, So it is very easy to merge code and avoid conflicts. It can also boost the development process and reduce development time and resources.

Benefits of Using Swift UI :

  • It’s easy to learn, and the code is simple and clean.
  • It can be mixed with UIKit using UIHostingController.
  • It allows you to easily manage themes. Developers can easily add dark mode to their apps and set it as the default theme, and users can easily enable dark mode.
  • SwiftUI provides mechanisms for reactive programming enthusiasts with BindableObject, ObjectBinding, and the whole Combine framework. That can reduce more code and process over variables.
  • It offers Live Preview. You do not need to run the app every time when you need to check layout. It makes the development process faster.
  • You do not need to care about @IBOutlet connections in swiftUI.
  • SwiftUI is fast and scalable.

Drawbacks of SwiftUI :

  • SwiftUI can only be used from the latest version of iOS 13 and up. SwiftUI alleviates this issue by integrating seamlessly with applications that are built and run with Interface Builder (such as UIKit, Storyboards, etc.). You can use SwiftUI views in Storyboard based User Interface, and vice versa. This is a very helpful tool in slowly transitioning an application to SwiftUI
  • The Swift was introduced in 2014, so there is not much data on Stack Overflow. It means that you can not get much help to solve complicated errors
  • It does not allow you to analyse the view hierarchy in Xcode previews.

Conclusion

SwiftUI has a completely different approach than UIKit to writing code. SwiftUI is clear, easy to read, and convenient to use. Definitely in near future developers will start to move with swiftUI application.

Even if you’re still a passionate supporter of UIKit, you can safely add functionality from SwiftUI to your UIKit projects. I hope this article has been useful and has answered the most common SwiftUI and UIKit related questions.

References :

https://developer.apple.com/xcode/swiftui/

--

--