Joshua Button - Big Nerd Ranch Wed, 01 Jun 2022 19:33:51 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 SwiftUI or UIKit: Which is Right For You? https://bignerdranch.com/blog/learning-apples-swiftui-or-uikit-which-one-is-right-for-you-right-now/ Tue, 02 Mar 2021 17:54:36 +0000 https://www.bignerdranch.com/?p=4729 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.

The post SwiftUI or UIKit: Which is Right For You? appeared first on Big Nerd Ranch.

]]>
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!

The post SwiftUI or UIKit: Which is Right For You? appeared first on Big Nerd Ranch.

]]>