Search

SwiftUI or UIKit: Which is Right For You?

Joshua Button

4 min read

Mar 2, 2021

iOS

SwiftUI or UIKit: Which is Right For You?

SwiftUI, Apple’s new declarative programming framework, was introduced along with iOS 13 in September 2019. As a student rolling into 2021 you may be wondering if you should start your iOS development journey with SwiftUI or pick the tried and true UIKit? Spoiler alert: it depends.

UIKit

UIKit provides a variety of objects which you can use to develop apps for iOS. These objects, such as UIView and its subclasses, allow for the display and interaction of content within your app. UIKit apps generally make use of the Model-View-Controller (MVC) design pattern.

UIKit has been the backbone of UI development on iOS for over a decade. It is a mature platform that sees use in just about every iOS application in existence. Since it is well established, there is an abundance of resources available in case you get stuck or have questions.

UIKit apps can be built in a few different ways:

  • Leveraging Interface Builder in order to design a UI without writing any code. Interface Builder is integrated into Xcode and allows for editing of .storyboard and .xib files, both of which describe layouts using XML.
  • A code focused approach where views and layout constraints are defined in Swift (or Objective-C).
  • A mix of the above two approaches.

SwiftUI

SwiftUI is Apple’s new declarative programming framework used to develop apps for iOS and Mac using Swift. The declarative approach is the key differentiator when comparing SwiftUI to UIKit. In UIKit you mediate the relationship between events and changes to presentation. With SwiftUI the need to mediate that relationship disappears since that is handled by the framework itself.

As far as building apps with SwiftUI, things are a bit more streamlined when compared to UIKit:

  • Xcode displays the visual editor alongside any file that contains a SwiftUI view, displaying a live representation of the view you are building. You can still interactively design on the canvas, just like in Interface Builder.
  • .storyboard and .xib files are not used in SwiftUI. The Swift code itself describes the layout rather than these opaque XML files.

Here are some additional notes and caveats:

  • Widgets built using WidgetKit are required to use SwiftUI. This is the only case as of now (iOS 14), but given Apple’s technology and API history, we anticipate a time will come when you will need to use SwiftUI in order to leverage the latest Apple features.
  • Swift developers can take advantage of Apple’s native libraries and design elements across all of its platforms. For example, you can’t do a native UI on watchOS if you don’t use SwiftUI.
  • SwiftUI requires iOS 13 or later. It might not be right for apps where backwards compatibility is important.
  • It wasn’t until iOS 14 and the introduction of the App and Scene protocols that SwiftUI became viable for building whole apps.

Using SwiftUI and UIKit Together

It is important to keep in mind that UIKit and SwiftUI are not mutually exclusive. It is possible to use UIKit code in a SwiftUI view and vice versa. Let’s take a look at some examples!

Using UIKit in SwiftUI

UIViewRepresentable is a protocol provided by the SwiftUI framework. Using this protocol it is possible to wrap an instance of a UIKit view so that it can be displayed with SwiftUI

Defining a UIKit view wrapper with UIViewRepresentable:

@available(iOS 13, *)
struct MyUIKitView: UIViewRepresentable {
    func makeUIView(context: Context) -> UITextView {
        // Creating, configuring, and returning the UIKit view here
        let view = UIView()
        view.backgroundColor = .blue
        return view
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
        // Update the state of the view here.
    }
}

Using the wrapped UIKit view in SwiftUI:

struct ContentView: View {
   var body: some View {
      VStack {
         Text("Hello from UIKit!")
         MyUIKitView()
      }
   }
}

Using SwiftUI in UIKit

UIHostingController is a view controller which allows for the display of SwiftUI views into a UIKit view hierarchy. It can be used just like any other view controller in UIKit:

let hostingController = UIHostingController(rootView: Text("Hello from SwiftUI!"))
present(hostingController, animated: true)

See Apple’s Documentation for more information.

The Bottom Line

Here at Big Nerd Ranch, we’ve had ample experience working with both UIKit and SwiftUI. We believe that UIKit is foundational to an iOS developer building apps on Apple platforms now and into the future. It is our recommendation that you start with that if you’re unsure of which to choose.

SwiftUI is a great choice assuming the minimum iOS version for the project is set to iOS 14. However, be aware that you may run into issues and limitations as you build your app. Having a foundation in UIKit would help you navigate these problems.

TL;DR

Learn UIKit. It’s a mature platform that can handle a wide range of user interfaces, from the simple to the complex. If you want to explore new frontiers, check out SwiftUI. Since they are not mutually exclusive, it’s even worth learning both!

Juan Pablo Claude

Reviewer Big Nerd Ranch

During his tenure at BNR, Juan Pablo has taught bootcamps on macOS development, iOS development, Python, and Django. He has also participated in consulting projects in those areas. Juan Pablo is currently a Director of Technology focusing mainly on managing engineers and his interests include Machine Learning and Data Science.

Speak with a Nerd

Schedule a call today! Our team of Nerds are ready to help

Let's Talk

Related Posts

We are ready to discuss your needs.

Not applicable? Click here to schedule a call.

Stay in Touch WITH Big Nerd Ranch News